I've read up on synchronization in Java and I just can't seem to figure out how to do this with only three classes! I think that I need one more class, in order to have a shared object that they can synchronize on, but I'm only allowed 3 classes in this assignment! Any help on how to get the 'more enhanced' version going with only three classes is very appreciated!

I could not understand the requirement clearly.
Can you explain what is the expected behavior .
what do you mean by
Stop the thread from T1
Wait for 5 seconds
Stop the thread from T2

If you can provide requirements clearly I will give a try.
Whether t1 and t2 should interleave or something
expected output or a dry-run please

kalle anderzon
Greenhorn

Joined: Mar 18, 2009
Posts: 3

posted Mar 19, 2009 02:51:39

0

Praveen Seluka wrote:Hi

I could not understand the requirement clearly.
Can you explain what is the expected behavior .
what do you mean by
Stop the thread from T1
Wait for 5 seconds
Stop the thread from T2

If you can provide requirements clearly I will give a try.
Whether t1 and t2 should interleave or something
expected output or a dry-run please

Hi Seluka!

Sorry if I was a bit unclear on the behavior.

For the 'slightly more advanced version'.

1. Instantiate and start a thread from class T1
2. Wait for 5 seconds (while waiting print out T1 once per second)
3. Instantiate and start a thread from class T2
4. Wait for 5 seconds (while waiting print out T1 (from T1) and T2 (from T2) once per second)
5. Pause the thread from class T2
6. Wait for 5 Seconds (while T2 is pausing T1 is still printing out T1 once per second)
7. Re-activate the thread from class T2
8. Wait for 5 seconds (T1 and T2 printing out once per second)
9. Stop the thread from class T1
10. Wait for 5 Seconds (only T2 still running, so T2 once per second)
11. Stop the thread from class T2

expected ST OUT should be sth like this (without the (ms) just added for clearer view of timecycle:

Nitesh Kant wrote:It looks like your assignment so i will not give you the code!

The following are my inputs:

The synchronized blocks in main are not required because no two threads execute the main method at any time.

The synchronized methods in T1 are not required as no two threads execute those methods.

I dont see a code for &amp;amp;quot;pause&amp;amp;quot;. You would require wait-notify for this. The monitor for wait-notify can be an arbitrary object instance. You do not need a new class for it.

Not sure why you do this:

Let me know if the above is not clear.

Hi Nitesh!

Thanks for the input! Especially "You would require wait-notify for this. The monitor for wait-notify can be an arbitrary object instance. You do not need a new class for it", I thought for sure that another class was needed.

However I'm not sure how to use it at the moment. wait() and notify() are methods in Object not Thread right?

This assignment didn't explicitly say that you had to use synchronized (or wait() notify() ) ,
I've come up with a solution that does what it's supposed to anyway.