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?????????????????????????????????/

I am no expert at Java threading, but I believe the main issue is that you aren't synchronizing on a single object, but rather you have three separate objects of the same type, which happen to be synchronized, but not with each other. You would need a single object which is synchronized in all three threads in order to control the order in which the threads interleave.

import java.lang.Runnable;

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;
    String s;
    public Checking(String s, Race1 ob){
        this.s = s;
        this.ob = ob;
    }
    public void run(){

        synchronized(ob){
            ob.show(s);
        }
    }
}

public class RaceCondition1{

    public static void main(String args[]){
        Race1 ob = new Race1();

        Thread t1=new Thread(new Checking("Hello", ob));
        Thread t2=new Thread(new Checking("World", ob));
        Thread t3=new Thread(new Checking("Complete", ob));
        t1.start();
        t2.start();
        t3.start();
        try{

            t1.join();
            t2.join();
            t3.join();
        }catch(InterruptedException e){

            e.printStackTrace();
        }
    }
}

i do not understand that what is scnerio and tell me one thing that,is there any difference to create object in main class and create object in Runnable

I think there is no difference in creating object I create object in Checking class and synchronized but you create object in main class and send object in constructor as arguments and then synchronized please clarify it what is the difference in this confusing situation and with your code my output is correct but how?????????????
sorry for bad english.

The difference is that when you were creating ob in Checking, were were creating an object in each thread, which meant that they weren't shared between the different threads - each thread was synchronizing a different object. You need to have a shared object to synchronize on if you are trying to provide mutual exclusion.

By constructing the single object in main(), and more importantly, passing that single object to all three threads, all three threads share the object. That is what the synchronized block is for - for controlling access to a shared resource. If you aren't sharing the resource, then synchronized will have no effect.

Edited 2 Years Ago by Schol-R-LEA

This question has already been answered. Start a new discussion instead.