I have one situation which I am not able to decipher the cource of action the program is taking while doing synchronization. If anyone cud explain me the process it wud be great.
class Thread2 {
static Thread2 Tr3 = new Thread2();
static Thread2 Tr4 = new Thread2();
static Thread Tr1 = new Thread(new Thread1("Tr1"));
//Tr1.setPriority(1);
static Thread Tr2 = new Thread(new Thread1("Tr2"));
//Tr1.setPriority(1);
public static void main (String args[]){
Tr3.somerun(Tr1);
Tr4.somerun(Tr2);
}
public static synchronized void somerun (Thread Tr){
Tr.start();
}
}
class Thread1 implements Runnable{
public String message[] = {"This", "is"," one", "of the Java programs"};
String name;
public Thread1(String id){
name = id;
}
public void run() {
for(int i = 0; i < message.length; i++)
{
System.out.println(name + message[i]);
//randomwait();
}
}
void randomwait(){
try{Thread.currentThread().sleep((long)(3000*Math.random()));}
catch(InterruptedException e){
System.out.println("Interrupted");}
}
}
I have basically tried to synchronized the somerun method. I have instantiated two objects of Thread2 class and tried to run the synchronized method to find out whether the other thread is blocked when the first one is operating. with the statements within the comments block the above program appear to run as expected and that is first Tr3 runs and the Tr4 runs. However if i change the Thread priority to a lower value for both of them then there does not seem tobe any sync at all. also if i add the randomwait() into the run() then also i dont see the effect of sync. can anyone help me?.
Thanks