This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

creating multiple threads

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Your mother was a hamster and your father was a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic