Results 1 to 3 of 3

Thread: Threads in Java

  1. #1
    ZeCute is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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.

  3. #3
    Drun's Avatar
    Drun is offline Member
    Join Date
    Nov 2009
    Location
    Indian Head, MD (Missing Pittsburgh PA)
    Posts
    44
    Rep Power
    0

    Default

    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
    Java 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
    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();
    in that way you will not have curing2 made till untill after curing1 has truely started.
    Michael P. O'Connor
    http://www.mikeoconnor.net

Similar Threads

  1. Java Threads Interview Question
    By _tony in forum Threads and Synchronization
    Replies: 20
    Last Post: 12-22-2008, 08:31 AM
  2. can you give-me a little hand about java threads?
    By masdrobeda in forum New To Java
    Replies: 3
    Last Post: 11-10-2008, 05:15 PM
  3. How to use Java threads
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •