Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

creating multiple threads  RSS feed

 
tomo black
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i'm hoping to generate a random number, check if it's even of not and pass odd/even as an arg to a newly created thread. but i dont quite understand how to do so( the thread part). i want to create dozens of threads.
can anyone help?

thanks tomo
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can pass arguments to threads via constructor. Just implement a class that implements interface Runnable (or extends class Thread) and pass whatever you like:



Stefan
 
tomo black
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi stefan, thanks for your reply.
i was creating the multiple threads like so but i read that you shouldn't use Runnable for multiple threads. This wont compile properly (cant recognise NEW symbol) and i'm not sure how to fix it.


myThread implements Runnable;
int x;
while(true)
{
x= randomNo();
if(x%2==0)
new myThread(true);//even
else
new myThread(false);//odd
}
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tomo black:
hi stefan, thanks for your reply.
i was creating the multiple threads like so but i read that you shouldn't use Runnable for multiple threads. This wont compile properly (cant recognise NEW symbol) and i'm not sure how to fix it.


Tomo, we can't really help you with compile problems, when you only show us pseudo code. Please give us the code you tried to compile.

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if "new myThread(true)" is real code, you need to create a Thread to use your runnable and start it. The line might look like:


Just to sharpen up your eye, do you see a way to pass true or false to your constructor without setting a variable and doing an if test? Deleting code is my favorite part of programming so reducing four lines to one would make me smile all day.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tomo black:
hi stefan, thanks for your reply.
i was creating the multiple threads like so but i read that you shouldn't use Runnable for multiple threads. This wont compile properly (cant recognise NEW symbol) and i'm not sure how to fix it.



That's false advise. Whether you use Runnable or not depends on design decisions, not the number of instances you're going to create.
Just be sure to create (unless you have specific requirements) a separate instance of your Runnable class for each Thread you create and you should be fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!