i write a code of multithreading in which arise an race condition code is below
class Race1{
public void show(String s){
try{
System.out.print("["+s);
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("]");
}
}
class Checking implements Runnable{
Race1 ob=new Race1();
String s;
public Checking(String s){
this.s=s;
}
public void run(){
synchronized(ob){
ob.show(s);
}
}
}
class RaceCondition2{
public static void main(String args[]){
Thread t1=new Thread(new Checking("Hello"));
Thread t2=new Thread(new Checking("World"));
Thread t3=new Thread(new Checking("Complete"));
t1.start();
t2.start();
t3.start();
try{
t1.join();
t2.join();
t3.join();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
but problem is that i handle it by synchronized block
in line no 27
and 28
but it is not handling why?? i don't know
its output is
[Hello[Complete[World]
]
]
output should be display
[Hello]
[Complete]
[Race]
whats the problem?????????????????????????????????/