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.
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?
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:
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 }
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.
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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.
42
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