• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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








 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    kalle anderzon
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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:





     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic