*
The moose likes Threads and Synchronization and the fly likes thread communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread communication" Watch "thread communication" New topic
Author

thread communication

nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Hi
i got a problem in multithreading. In my project I have 10 threads and each thread shud run one after another ie 1,2,3,4------,10 after the completion of the 10 thread again the 1 thread shud run.For inter thread communication I know that wait and notify are one means but how to give the preference if many threads are there. Is there anything like thread priority. I will be thankful if someone provides me with code
bye
nitin
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Nitin,
you can set a Thread's priority by using its setPriority(int) method.
However,be forewarned that setting one thread to a higher priority does not guarantee that thread will finish before any other thread is started.


Fun programming etcetera!
Paulo Salgado
Ranch Hand

Joined: Jan 18, 2002
Posts: 98
Nitin,
you want to restart T1 after all ten threads are dead, is that right ? (considering T8 can finish after T9, for instance)
If that's the case, in the loop where you'll be doing the restart, you can check if there's still threads alive. For instance:

If you just want to check for the tenth Thread then replace the for loop in the do-while by yourThread[9].isAlive();
[ August 23, 2002: Message edited by: Paulo Salgado ]
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
With respect to restarting threads......I always thought that you cannot restart a dead thread.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Dave Winn:
Nitin,
you can set a Thread's priority by using its setPriority(int) method.
However,be forewarned that setting one thread to a higher priority does not guarantee that thread will finish before any other thread is started.
With respect to restarting threads......I always thought that you cannot restart a dead thread.

True and true. However, it is also possible to cheat . See the sample code below:

Ok, so it's not elegant, but I did it in a hurry...
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by nitin kumar:
Hi
i got a problem in multithreading. In my project I have 10 threads and each thread shud run one after another ie 1,2,3,4------,10 after the completion of the 10 thread again the 1 thread shud run.For inter thread communication I know that wait and notify are one means but how to give the preference if many threads are there. Is there anything like thread priority. I will be thankful if someone provides me with code
bye
nitin

Why are you using threads in the first place? You have pieces of code (10 of them) that must run one after the other. Once the last one is complete, you run the process again. Wouldn't one thread do the trick here with a looping construct of some sort?


James Carman, President<br />Carman Consulting, Inc.
Paulo Salgado
Ranch Hand

Joined: Jan 18, 2002
Posts: 98
James, that was the reason of my first assumption.
Dave, ooooppssss... completely right. It wouldn't work this way. The threads in the array would have to be reallocated at every loop iteration.
Anthony, that's a cool piece of code.
[ August 27, 2002: Message edited by: Paulo Salgado ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
So sorry friends but this code is not cool at all . On second look it's logic is faulty. It only looks as if it's working because the threads are started in the desired order and they all have the same default priority. Anyway I'll try to make a real solution when I'm free.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm with James Carman here; I'm not convinced that recent posts have anything to do with what Nitin originally asked about; which doesn't seem to really require threads at all. But I do note that if you do want to wait until all ten threads are dead, there's a simple solution which does not keep bothering the processor when nothing has happened:

The join() method waits patiently for a thread to die; if it's already dead, the method completes without waiting.
My time is short, so I'll leave Anthony alone while he works on his code. Except to note that Doug Lea's Concurrent Programming in Java may have some relevant ideas (if you haven't already looked there). Cheers...
[ August 27, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Okay, I promised to give something, so here it is.
The premise I was acting on was that 10 threads would access the same object in a certain order: A first, then B, C, etc until J and then A again ad nauseam. Whether or not this is actually a valid interpretation of Nitin's problem, I am no longer inclined to argue. I just want to see if this premise can be implemented.
I deliberately initialized the threads in reverse order and set them in "opposing" priorities to test if the code really did what was asked. It also successfully sifted out thread A to access the common object first, even though it had the least priority.
I didn't clean up the code anymore to be "tighter" and more elegant; i just wanted to see if it would work. Bear with me
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
:sigh: I really hate it when the (;; ) for loops do that...
Also, swap

to

[ September 01, 2002: Message edited by: Anthony Villanueva ]
Peter Haggar
author
Ranch Hand

Joined: Jan 03, 2001
Posts: 106
Originally posted by nitin kumar:
Hi
i got a problem in multithreading. In my project I have 10 threads and each thread shud run one after another ie 1,2,3,4------,10 after the completion of the 10 thread again the 1 thread shud run.For inter thread communication I know that wait and notify are one means but how to give the preference if many threads are there. Is there anything like thread priority. I will be thankful if someone provides me with code
bye
nitin

To solve this problem you can use what's called specific notificaiton. It's pretty simple and is described, with sample code here.
Peter Haggar


Senior Software Engineer, IBM
author of: Practical Java
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A much more sober approach of course, was already suggested by both James and Jim. Only one thread is made at a time and run, to avoid mucking around with the scheduler. The code is cleaner and more elegant.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But if you don't want any concurrency, why use threads at all?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Ron Newman:
But if you don't want any concurrency, why use threads at all?

Oh I quite agree. Why use ten when one will do? It was purely an academic exercise
Hmmm... maybe I should take up a hobby...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: thread communication