aspose file tools*
The moose likes Threads and Synchronization and the fly likes Synchronizing between two threads, one implements Runnable the other extends Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronizing between two threads, one implements Runnable the other extends Thread " Watch "Synchronizing between two threads, one implements Runnable the other extends Thread " New topic
Author

Synchronizing between two threads, one implements Runnable the other extends Thread

kalle anderzon
Greenhorn

Joined: Mar 18, 2009
Posts: 3
Hey guys!


I've been a fan of this site for a while and now I'm joined and writing my first post.

I'm working on a basic program.
It is supposed to have three classes:

One main class (with the main method)
One class, T1, which extends Thread
One class, T2, which implements Runnable.

When the main class runs, it is supposed to:

Create and start a thread from class T1
Wait for 5 seconds
Create and start a thread from class T2
Wait for 5 seconds
Stop the thread from T1
Wait for 5 seconds
Stop the thread from T2

Once per second each thread should print out:
T1: T1
T2: T2

This is my code so far, it does what it is supposed to but I know it's ugly as hell.

A 'more enhanced' version of this program should be able to.

Create and start a thread from T1 class, Wait for 5 seconds
Create and start a thread from T2 class, Wait for 5 seconds
Pause thread from T2 , Wait for 5 seconds
Resume thread from T2 class, Wait for 5 seconds
Stop thread from T1 class, Wait for 5 seconds
Stop thread from T2 class

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!

Best regards

// oVERGROW








Praveen Seluka
Ranch Hand

Joined: Jul 17, 2007
Posts: 95
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
kalle anderzon
Greenhorn

Joined: Mar 18, 2009
Posts: 3
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:

T1 (initiated)
T1(1000ms)
T1(2000ms)
T1(3000ms)
T1(4000ms)
T1(5000ms)
T2 (initiated)
T1
T2 (1000ms)
T1
T2 (2000ms)
T1
T2 (3000ms)
T1
T2 (4000ms)
T1
T2 (5000ms - pausing T2 thread here)
T1 (0 ms)
T1 (1000 ms)
T1 ( 2000 ms)
T1 ( 3000 ms)
T1 ( 4000 ms)
T1 (5000 ms)
T2 (0 ms - re-activiting thread from class T2 here)
T1
T2 (1000 ms)
T1
T2 ( 2000 ms)
T1
T2 (3000 ms)
T1
T2 ( 4000 ms)
T1
T2 ( 5000 ms)
T1 (stopping Thread from class T1 here)
T2 (0 ms)
T2 (1000 ms)
T2 ( 2000 ms)
T2 ( 3000 ms)
T2 (4000 ms)
T2 (5000 ms - stopping thread from class T2 here)



// kalle


Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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 "pause". 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.


    apigee, a better way to API!
    kalle anderzon
    Greenhorn

    Joined: Mar 18, 2009
    Posts: 3
    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 "pause". 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.


    updated code:





     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Synchronizing between two threads, one implements Runnable the other extends Thread