-6

I am having problem dealing with synchronization java threads, applying wait and notify..

I want to figure out how could I implement these in a program where I can print out the answer alternately.. for example person1 will count numbers 1-5 as well as person2, the output should be like this.

person1 count 1
person2 count 1
person1 count 2
person2 count 2
person1 count 3
person2 count 3
person1 count 4
person2 count 4
person1 count 5
person2 count 5

Thanks guys.

Gray
  • 115,027
  • 24
  • 293
  • 354
xknozi
  • 1,335
  • 3
  • 12
  • 20
  • You cant do this. The java scheduler has its own algorithm to decide which thread to run. SOunds like you want a single threaded loop. – RNJ Oct 05 '12 at 13:50
  • Do you want us to tell you all about threads in Java ? Do you want us to write a program for you ? Can you be more specific ? – Tulains Córdova Oct 05 '12 at 13:51
  • 2
    In Java 7 you could use the `Phaser` for this. Prior to Java 7 the best fit would probably be `CountDownLatch`. But, if this is not just a simplification of some real concurrent scenario, there is no reason to use multithreading. – Marko Topolnik Oct 05 '12 at 13:53
  • Java threads aren't meant to do do stuff like this. You could do it using locks, however you'd have to be very cautious not to create deadlocks. Personaly i would implement this totally different. – G-Man Oct 05 '12 at 13:54
  • Hey guys thanks for responding. I don't want you to write the whole program, I just need your idea on what should I do in order to achieve that output. I have a hard time understanding java threads, but I will give more time to understand it. I am just new to java and I want to learn. and with your help, I know I can do it. Thanks guys for the respond. I will update you in my progress. Thanks. – xknozi Oct 05 '12 at 23:57

3 Answers3

1

You could do this easily in two ways:

  1. Pass a 'print token' between the threads using two semaphores: thread 1 prints, signals semaphore A, waits on semaphore B then loops. Thread 2 waits on semaphore A, prints, signals semaphore B and loops.

  2. Write in-line, single-threaded code.

Gray
  • 115,027
  • 24
  • 293
  • 354
Martin James
  • 24,453
  • 3
  • 36
  • 60
  • I think that would be the solution in my problem. but i have no idea in semaphore, can you give me links where tutorials and examples were provided? thank you. – xknozi Oct 05 '12 at 23:58
0

Don't use wait and notify. Use syncronized blocks.

For an in-depth explaination of how the Java Monitor works, including code examples, you can go here: http://www.artima.com/insidejvm/ed2/threadsynch.html

Mikkel Løkke
  • 3,710
  • 23
  • 37
  • That does not answer the question - in particular I fail to see how using a synchronized block will alternate the threads. – assylias Oct 05 '12 at 13:55
  • Technically `wait/notify` includes the use of `synchronized` blocks since you need to hold the lock in order to call `wait/notify`. – Tudor Oct 05 '12 at 13:55
  • Tudor: I know that synchronized technically just wraps wait/notify, I just think the semantics are cleaner and easier to understand. assylias: The treads will alternate in the order they are prioritized by the monitor. – Mikkel Løkke Oct 06 '12 at 15:15
0

The whole purpose of threaded programs is asynchronous operation of the threads. That's how you get a performance boost because the different tasks can work on different CPUs/cores concurrently, without having to synchronize on each other. To force this sort of synchronized, lock step output is by definition forcing threads to do something atypical.

@Martin's answer provides alternatives to get it to work.

Gray
  • 115,027
  • 24
  • 293
  • 354