aspose file tools*
The moose likes Threads and Synchronization and the fly likes a multithreaded program by creating a subclass of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "a multithreaded program by creating a subclass of Thread " Watch "a multithreaded program by creating a subclass of Thread " New topic
Author

a multithreaded program by creating a subclass of Thread

fazae vain
Greenhorn

Joined: Feb 14, 2013
Posts: 5
I want The threads will execute the following output in the suitable interface.

like this OUTPUT

thread1: Java
thread1: is
thread2: Java
thread1: an
thread2: is
thread1: exciting
thread2: an
thread1: new
thread2: exciting
thread1: language
thread1: for
thread1: concurrent
thread2: new
thread1: programming.
thread2: language
thread2: for
thread2: concurrent
thread2: programming.



but mine not display like that output
why ?


My output :

thread1: Java
thread2: Java
thread2: is
thread1: is
thread2: an
thread1: an
thread2: exciting
thread1: exciting
thread2: new
thread2: language
thread2: for
thread2: concurrent
thread1: new
thread2: programming
thread1: language
thread1: for
thread1: concurrent
thread1: programming




Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What exactly is your problem with the output? The first thing you should realize is that you should not be worrying about any one single order. The threads are running concurrently so the order can come out in any pattern. It looks like you expected intermingling, and you get intermingling. So I am not exactly sure where the problem is. Can you please explain what in the output you don't expect?


Steve
fazae vain
Greenhorn

Joined: Feb 14, 2013
Posts: 5
this is my assignment from my lecturer,she want excactly like her output. i know the thread will come out randomly -_- thats why i dont understand.


thread1: Java
thread1: is
thread2: Java
thread1: an
thread2: is
thread1: exciting
thread2: an
thread1: new
thread2: exciting
thread1: language
thread1: for
thread1: concurrent
thread2: new
thread1: programming.
thread2: language
thread2: for
thread2: concurrent
thread2: programming.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Ask your professor to clarify the requirement. You can make it get the output she wants exactly, but it is a lot of synchronizing work and may not be what she actually wants.

If it is what she wants then you have to:
- Determine which thread you are in
- Compare the current word with the words that this thread should yield to the other thread.
- Use a synchronization token to notify the other thread
- Wait on the same synchronization token
fazae vain
Greenhorn

Joined: Feb 14, 2013
Posts: 5
okay i'll tq steve
 
 
subject: a multithreaded program by creating a subclass of Thread