-2
public class suspendCheck extends Thread{

    int t ;

    public static void main(String args[]) throws InterruptedException {
        suspendCheck as = new suspendCheck();

        Thread t2 = new Thread(as);
        t2.start();
        Thread t3 = new Thread(as);
        t3.start(); }

    public void run(){
        if(t==0){
        System.out.println("JAVA Develper");
        t= ++t;
        }
        else{

            System.out.println("JAVA Test");
            }

        }
}

After running this program multiple times there are differences and discrepancies:

Usually:

JAVA Develper
JAVA Develper

And occasionally:

JAVA Develper
JAVA Test

I am horrified by this. Where did I go wrong?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Aslam a
  • 750
  • 10
  • 19
  • 1
    Welcome to the world of thread synchronisation. – Mitch Wheat Sep 01 '13 at 08:21
  • 6
    Yes, it is acceptable. The only horrifying thing here is that you attempt to use thread without knowing their limitations. Blaming Java isn't the right thing to do here. – nanofarad Sep 01 '13 at 08:22
  • I just don't understand why people gave -1 for this question, he just has no experience in multithreading – anvarik Sep 01 '13 at 09:13

4 Answers4

7

Yes, it is acceptable. The only horrifying thing here is that you attempt to use thread without knowing their limitations.

You could easily do this with one thread. You could take the safe path here.

Threads do not guarantee anything regarding optimizations and ordering. If you really want your int to change properly, make it volatile and/or use an AtomicInteger. If you want even better control use locks and/or synchronized blocks.

In this case, just declare t as volatile and synchronize around accesses to t or AtomicInteger and use that class's methods to access t.

For future reference, trying things like these without reading all associated warnings can lead to death, asphyxiation, chills, fever, drowing, infection, nausea, and the inability to control highly expensive machinery.

Community
  • 1
  • 1
nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

i just want to add to hexafraction answer that you should read on race condition:

and to explain that what you did in your code is to make several threads to try to take an integer and change it in a way that one of them may change it while the other thread is taking it and thus override a value given by the first thread. that's why you're getting this behavior

Race condition in Java is a type of concurrency bug or issue which is introduced in your program because parallel execution of your program by multiple threads at same time, Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

You cannot guarantee the order of execution of threads.When you call start() method on a thread, it is now upto the thread scheduler to what should be the order of execution

t1.start();
t2.start();

This doesn't imply t1 will definitely run before t2. In your example, you are not synchronizing the method,both the threads may be running simultaneously,or by the time one thread comes to line 4, the other may have already finished executing.

 public void run(){
        if(t==0){
        System.out.println("JAVA Develper");
        t= ++t;    //Line 4
        }
        else{

            System.out.println("JAVA Test");
            }

        }

Synchronized methods will provide the thread a lock on an object.No other thread having the same object as runnable ,will be able to access the synchronized block or a method simultaneously .

 synchronized public void run()
Malwaregeek
  • 2,274
  • 3
  • 15
  • 18
0

you should use threads if stuff needs to happen in parallel, in stead of serial. Unless you explicitly synchronise, which kind of breaks the idea of threads, things will happen in an undefined order.

Understanding threading and getting it absolutely right is both critical and the single most difficult thing to do in just about any language.

I'd recommend reading java concurrency in practice by joshua bloch to see just how many pittfalls there are when using threads and, more importantly, sharing state.

extraneon
  • 23,575
  • 2
  • 47
  • 51