0

Consider this simple try for a multithreading example :

public class LetsMutexThreads {

    public static Object MUTEX = new Object();

    private static class Thread1 extends Thread {
        public void run() {
            synchronized (MUTEX) 
            {
                System.out.println("I'm thread 1 , goint to take a nap...");
                try 
                {
                    MUTEX.wait();
                } 

                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }

                System.out.println("T1 : That's it , I'm done ...");
            }
        }
    }

    private static class Thread2 extends Thread {
        public void run() {
            synchronized (MUTEX) 
            {
                System.out.println("Thread 2 : Let's rock N roll !");
                System.out.println("Waking up my buddy T1 ...");
                MUTEX.notify();
            }
        }
    }

    public static void main(String[] args) 
    {
        Thread2 t2 = new Thread2();
        Thread1 t1 = new Thread1();
        t1.run();
        t2.run();
    }

}

I'm trying to allow Thread1 to go to sleep with the wait , and then let Thread2 to use notify() to wake Thread1 , but he doesn't get a chance .

Why does the wait() of Thread1 affects the Main Thread from executing t2.run(); ?

JAN
  • 21,236
  • 66
  • 181
  • 318

2 Answers2

4

You must not attempt to start a thread using the run() method. It does not actually create a new thread, it runs the code in the current thread. Use thread.start() instead in order to have your code executed in a separate (new) thread

alampada
  • 2,329
  • 1
  • 23
  • 18
  • 1
    Right idea, but "should" makes it sound like the caller has a choice when in fact, there is no such choice. The _only_ way to start a thread is to call `t.start()`. – Solomon Slow Sep 18 '15 at 14:26
  • Thanks for the comment, I have updated my answer to make it more clear. – alampada Sep 18 '15 at 14:33
3

This is wrong:

Thread2 t2 = new Thread2();
Thread1 t1 = new Thread1();
t1.run();
t2.run();

Change it to this:

Thread2 t2 = new Thread2();
Thread1 t1 = new Thread1();
t1.start();
t2.start();
dsh
  • 12,037
  • 3
  • 33
  • 51
  • Your first snippet is not _wrong_ Not in and of itself. It's only wrong if it fails to do what the programmer intended it to do. Your answer doesn't say anything about the programmer's intent. Your answer doesn't say _why_ the first example is wrong or why the second one is right. All you're doing here is throwing the noob a fish when you could be teaching him _how_ to fish. – Solomon Slow Sep 18 '15 at 14:35
  • Okay. Is it ever right to create a Thread object (instead of a Runnable) and then call run()? I can't think of any situation where it would be desirable. I hope the reader sees the difference in the two code snippets and looks up the documentation on the two methods to understand the rest. – dsh Sep 18 '15 at 15:03
  • "Right" as in good design? I can't imagine how a call to `t.run()` could ever improve the _design_ of a program, but I can imagine it as part of a cheap, sleazy hack that fixes a bug. I've seen worse. Heh! I've probably _committed_ worse, and in commercial products too. As for your hope, don't worry about it. Antonis_wrx already gave the noob the information that he needs. – Solomon Slow Sep 18 '15 at 16:48