Question : Java synchronized Thread issue

Hi experts

We have an issue with synchronized methods and synchronized objects with threads

We have correctly have and test code which we fire socket requests into which then creates an thread what goes into an synchronized lock which does some dummy Test proccess.

We expect with synchronized process going through code to be 1-thread 2-thread 3-thread 4-thread but we are currently having issue where the threads are going through the synchronized method in this order 1-thread 4-thread 3-thread 2-thread.
(the reason the 1-thread process first as it’s the first thread to hit the synchronized lock)

The java version we are using are:
java version "1.5.0_16"
Java(TM) Platform, Standard Edition for Business (build 1.5.0_16-b02)
Java HotSpot(TM) Server VM (build 1.5.0_16-b02, mixed mode)

We have done some digging around and found that there sort of an bug on java side
http://bugzilla.globus.org/globus/show_bug.cgi?id=5583

But we are unable to update the java version JVM as other applications are using this version.

So for the question:

1. Is there another of way of using an locking method with the current version of java what will work with multiple threads accessing it what will make threads go in order?

2. We need an array locks so we can control certain threads doing the methods to process data.
             For example Thread-1 is accessing an method gets an lock[0]
                                  Thread-2 is accessing an method gets an lock[1]
                                  Thread-3 is accessing an method gets an lock[0] (blocked to Thread-1 is finished as it needs to the same procces the same as Thread-1)
                                  Etc…

Thanks in-advance,

p.s. are test code is below with the issue with the thread ordering (any socket request will run the code on the correct port)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
import java.net.*;
import java.io.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class Test extends Thread implements Runnable {
  public Test() {
  }


//as gets a reponce create a thread strate away then start listioning again

  //=========================================================================
  ServerSocket svrSocket;
  int clientCount;
  Socket clientSocket;
  public boolean ExitCommand = false;
  int IMIStatsFlag = 1;
  int PortNumber = 20009;
  int QueSize = 1000;
  static Object[] mutex = new Object[100];
  static Object mutex1;


  public static void main(String arg[]){
   System.out.println("Start");
mutex[0] = 0;


   Test T1 = new Test();
   T1.start1();
    System.out.println("Stoped");
 }


  public void start1()
  {
      int iclientCount = 120;       // Number of threads started

mutex1 = 1;

      try {
          svrSocket = new ServerSocket( PortNumber, QueSize );
      } catch (Exception /* IOException */ e) {
		  System.out.println(e.toString());
              return;
          }
          while( IMIStatsFlag == 1 )
          {

          try {
              while( true ) {

                  clientSocket = svrSocket.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
			String XMLString = in.readLine();
			in.close();
            clientSocket.close();


            Test test = new Test();
			test.start();



              }
          } catch (IOException e) {
                  // Error
                  System.out.println("MIStatsListener ,Start ,IO Error: "+ e.toString());
          }
      }

  }



    public void run() {
			System.out.println(Thread.currentThread()+ " Before Lock");
		LockTest(0);
	}



  public  void LockTest(int MutexLockNumber) {


		try{
			Thread.sleep(10);

		}catch(Exception ex){
		}
	System.out.println(Thread.currentThread()+ " Before Lock");
	System.out.println(Thread.currentThread()+ " Thread count" + Thread.activeCount());

	synchronized(mutex[MutexLockNumber]){
	//synchronized(mutex1){

	System.out.println(Thread.currentThread()+ " Inside Lock");
	int x =0;

	while(x != 10){


		System.out.println(Thread.currentThread()+" "+x);
		x++;
		try{
			Thread.sleep(1000);

		}catch(Exception ex){
		}
	}
	System.out.println(Thread.currentThread()+ " Lock Finnshing");
}
	System.out.println(Thread.currentThread()+ " Lock Leaving now");


    }



}

Answer : Java synchronized Thread issue

Random Solutions  
 
programming4us programming4us