Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread communication

 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With respect to restarting threads......I always thought that you cannot restart a dead thread.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
: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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you don't want any concurrency, why use threads at all?
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic