Peculiar behaviour of synchronize?

I have made a method "synchronize" still I can't see the output as desired. I think a series of Thread-1 and series of Thread - 0 should come, but the output is coming in the random fashion. Can anyone please explain this behaviour of threads in java?

package threads;

publicclass SimpleThread

{

publicstaticvoid main(String[] args)throws InterruptedException

{

System.out.println(" the current thread is ... " + Thread.currentThread().getName());

Thread2 t1 =new Thread2();//t1.setDaemon(true);

t1.start();

Thread2 t2 =new Thread2();//t2.setDaemon(true);

t2.start();

System.out.println(" the current thread is ... " + Thread.currentThread().getName());

}

}

class Thread2extends Thread

{

publicsynchronizedvoid test()

{

//synchronized (this)

{

for (int i = 0; i <100; i++)

{

System.out.println("the current thread is ..." + Thread.currentThread().getName());

}

}

}

publicvoid run()

{

test();

}

}

Output is:

the current thread is ... main

the current thread is ... main

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-0

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

the current thread is ...Thread-1

[10316 byte] By [ritu_jainna] at [2007-11-27 9:36:31]
# 1
Both threads are synchronized on 'this'. As there are two threads there are two values for 'this', so they are synchronized on different objects, so they can interleave.
ejpa at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 2

EDIT:

The following is incorrect. See replies 11-14 for why.

Sorry for any confusion.

-

-

And even if they were using the same lock, they could still interleave.

The scheduler doesn't execute one statement per thread. If it did, there'd be no point to having multiple threads--you could just code those statemets sequentially.

It has its own rules for when to switch threads and which thread to switch to--time slicing, priority, blocking on I/O, etc.

-

-

Message was edited by:

jverd

jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 3

thanks. but the code of synchronize(this) is commented in the posted code. please let me know how can i get the functionality that the thread 0 waits for the monitor unless and until the thread 1 completes it. (or vice versa),. means i only want one thread to complete the processing on the method and then release the monitor to the other.

ritu_jainna at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 4
Try making your test method static. That way both threads will be trying to access the same method.
floundera at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 5
A synchronized method is also synchronized on 'this'.If you want to synchronize on the class, make the synchronized method static, or synchronize on this.getClass(), or on a static object in the class.
ejpa at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 6
i dont understand the usage of synchronize then? synchronizing a method guarentees that the method would be executed only by a single thread at single point in time. unless and until the lock is released, it wont be executed by other thread. is it correct?
ritu_jainna at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 7

> thanks. but the code of synchronize(this) is

> commented in the posted code. please let me know how

> can i get the functionality that the thread 0 waits

> for the monitor unless and until the thread 1

> completes it. (or vice versa),. means i only want

> one thread to complete the processing on the method

> and then release the monitor to the other.

There's no point in using multiple threads for that, but if you want to do that, then you'll have to use wait(), notifyAll(), and a variable that keeps track of whose turn it is.

I'll leave you to try to work out the details for yourself and then post again when you get stuck.

jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 8
You have 2 Thread2 objects, each has it's own test method. So synchronizing them is pointless as each Thread2 object is accessing its own copy of the test method.
floundera at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 9

> i dont understand the usage of synchronize then?

> synchronizing a method guarentees that the method

> would be executed only by a single thread at single

> point in time.

No, it doesn't.

Synchronizing a non-static method is simply the same as putting synchronized (this) {} around the entire body of the method.

synchronized (X) simply waits until X's lock is available, then obtains that lock.

Therefore, a synchronized non-static method means that no other thread can execute that method on that object.

Take some time to let that sink in. (HA! "Sink", "sync". Get it?)

jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 10
One thing I'm not clear on:I thought you wantedT1T2T1T2...But now I'm thinking you might want all of T1 and then all of T2.Either way, it's pointless to use multithreading.
jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 11

Your code would be more comprehensible if it didn't extend Thread. This is causing you some confusion. Try it like this:

// in main() ...

Runnable r = new Runnable2();

new Thread(r).start();

new Thread(r).start();

// Delete the Thread2 class and replace it with this ...

class Runnable2 implements Runnable

{

public synchronized void run

{

//synchronized (this)

{

for (int i = 0; i <100; i++)

{

System.out.println("the current thread is ..." + Thread.currentThread().getName());

}

}

}

}

ejpa at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 12
> Your code would be more comprehensible if it didn't> extend Thread. This is causing you some confusion.Note, however, that the order will still be unpredictable for the reasons described above.
jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 13
Bzzt. There is only one instance of the Runnable, so its synchronized run() method will run to completion on one of the threads before the other thread enters it.
ejpa at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 14

> Bzzt. There is only one instance of the Runnable, so

> its synchronized run() method will run to completion

> on one of the threads before the other thread enters

> it.

Oops. You're right of course.

I didn't read closely enough and thought the syncing was done inside the loop.

jverda at 2007-7-12 23:05:46 > top of Java-index,Java Essentials,New To Java...
# 15
OTOH, the order between the threads isn't predictable. It is likely but not certain that the first thread will enter run() first.
ejpa at 2007-7-21 23:04:42 > top of Java-index,Java Essentials,New To Java...