RocketMan on 26/7/2004 at 00:35
Heya....I'm learning Java 2 for the first time and so far so good, i'm liking it a lot.....however i just finished reading about threads and i made my own program with threads that worked but suddenly realized......what if i want say 3 threads to run in a given class that i make....
I know how to proceed when there is only one thread other than the main process...
Thread runner = new Thread(this);
runner.start();
Run() {
...
}
ok fine but if i defined other threads for example:
Thread runner2 = new Thread( ?????
Thread runner3 = new Thread(?????
what is the instance i use for these threads...is it the same instance as for runner?...how bou the run method....there's only one of those but i want each thread to do something different and the interface runnable only has a single run() method right?...i can't rename it or anything....how would i initialize 3 different threads, concurrently, in the same class environment, without having all three call the same run method???...any help would be much appreciated. :thumb: BTW...IMHO JAVA is cool :D
RocketMan on 26/7/2004 at 02:34
oh and here's the source code of the program [a trivial one at that] i wrote using threads...in case this helps illustrate the point better to me..thanx:
import java.*;
class test2 implements Runnable {
public static void main(String args[]) {
int j=500;
test2 chris = new test2();
Thread runner = new Thread(chris);
runner.start();
int k=0;
boolean garbage = false;
boolean stallfortime = true;
while(j>0) {
System.out.println(" " + j);
j--;
}
}
public void run() {
int i=0;
boolean finished = false;
while(finished==false) {
while(i<500) {
System.out.println(i);
i++;
}
finished = true;
}
}
}
Tej on 26/7/2004 at 06:07
I'm a bit rusty, meaning I haven't done any advanced Java in years... but I think I do remember things pretty much accurately. When you make multiple instances of a Thread, then each instance has its own Run method, so there should be no problem as far as that is concerned. You just have to let each instance know which one it is (via some class property, say runner2.index = 2; runner3.index = 3;).
With the Runner interface, I'm not so sure, but the main() method is static, therefore it doesn't depend on an instance, and in it you create the kind of instances you want. It may be more complicated though as it might expect only one instance... but I'd have to do more research on that one.
RocketMan on 26/7/2004 at 23:42
Ok i think i get what u are saying kind of but as far as the code is concerned i don't know what to do....how do u use that index thing to actually make different run() methods does each run method take an arguement or something?....if u could provide a snippet of code that illustrates how to use this indexing to make multiple threads i'm sure that would be clear to me and much appreciated..thanks again. :)
RocketMan on 28/7/2004 at 02:30
Oh BTW if anybody has a program written using multiple threads who wouldn't mind showing it to me that'd be great too.
RocketMan on 31/7/2004 at 17:17
I found some forum on the net where this guy posted a program of his using many threads in the same class and he used the runnable interface as i did. He used some sort of an index as an arguement for his threads to differentiate the run methods from each other but i have no idea what the hell is going on in the code...anybody know how to do this?...that is does anyone know how to use some sort of index to label different run methods so that different threads can run at the same time using their own unique run methods all in the same class?
RocketMan on 30/8/2008 at 18:41
NEW QUESTION:
I'm trying to figure out how to use Java to interface with the serial port of a computer so that, on command I can ask one of the pins to go to 5 volts for example. I'm pretty sure the serial port is able to do this and I'm pretty sure there's a class structure that handles hardware I/O but I don't know how to implement it in the code. What I'm trying to do is get the computer running the program to toggle a MOSFET on and off, which will be switching current on/off to a rocket launcher. As long as the code is able to turn one pin from 0-5 volts I'm happy. Anybody know how to do this?
RocketMan on 31/8/2008 at 23:29
Thanks for the reply. I did look on google for this issue and found the first link you posted...as well as others. I understand there are classes to control the serial port (i shouldn't have said "pretty sure"). I'm not versed well enough in computer hardware or in java itself to know how to actually use the class to do what I want it to do though. What I am most interested in is a snippet of code that controls a particular pin....really the most basic functionality that simply sets up the serial port to recieve commands from the program and then to be able to control the voltage on the pin. I suppose I could sift through the many examples of code out there or learn how to use the class with some sort of tutorial but I'm really not interested in the serial port for any other reason whatsoever so a code snippet would be ideal. Of course I might be oversimplifying the problem.
dvrabel on 1/9/2008 at 13:19
The serial port isn't suitable because its signal levels are -5V and +5V. You will want to use the parallel port.
Also, shouldn't your thread starting code look like this:
test2 chris = new test2();
chris.start();