-
Threads in Java
I have a little problem with threads in JavaME.
I shall create an application where the user should be able to start and stop an stopwatch. The application should include 5 different stopwatch, with different start and stop times.
I want to do it with threads.
I create a class which has the task of determining the start time:
public class Run (Implement runnable
public static long tid1;
public void run () (
tid1 = System.currentTimeMillis ();
)
)
In the second class I instantiated the class, and creates a number of threads.
curling1 = new Thread (new Run ());
curling2 = new Thread (new Run ());
curling3 = new Thread (new Run ());
Then start the threads
curling1.start ();
curling2.start ();
curling3.start ();
But I will not start all the threads in the same place which means I want to get the different start times. I would then get the value from the variable tid1 at the different start times. Howe van make it work?
-
Quote:
Howe van make it work?
First you need to do some more desk work to design what you want the program to look like (its GUI) and what it should do in response to user actions. Then you can get to the coding threads bit.
-
Just because you "start" a thread before a diffent thread does not mean the exicution of said thread started. When you do thread programming you get inditerminate running based on time, your best bet (if you want them to have the start time assending from the time you start them) is to set a varable varable in them when you create them and not to start the others till the varable has changed in the originnal
bad coding practice comming
You could in thory do something like the following
Code:
public class Run Implement runnable
{
public static long tid1
boolian timeSet = false;
public void run (){
tid1 = System.currentTimeMillis ();
timeSet = true;
}
boolian getTimeSet(){return timeSet;}
}
then
Code:
curling1 = new Thread (new Run ());
curling1.start();
while(!curling1.timeSet());
curling2 = new Thread (new Run ());
curling2.start();
while(!curling2.timeSet());
curling3 = new Thread (new Run ());
curling3.start();
in that way you will not have curing2 made till untill after curing1 has truely started.