Results 1 to 3 of 3
Thread: Threads in Java
- 05-20-2010, 11:29 AM #1
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
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?
- 05-20-2010, 03:54 PM #2
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.Howe van make it work?
- 05-20-2010, 04:06 PM #3
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
thenJava 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;} }
in that way you will not have curing2 made till untill after curing1 has truely started.Java 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();
Michael P. O'Connor
http://www.mikeoconnor.net
Similar Threads
-
Java Threads Interview Question
By _tony in forum Threads and SynchronizationReplies: 20Last Post: 12-22-2008, 08:31 AM -
can you give-me a little hand about java threads?
By masdrobeda in forum New To JavaReplies: 3Last Post: 11-10-2008, 05:15 PM -
How to use Java threads
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks