Click to See Complete Forum and Search --> : Regarding Threads


kendel
November 18th, 2009, 09:25 PM
Ok so I am making a gui using netbeans ide. I created thread class that is basically a timer.


public class Counter extends Thread
{
// time starts initially at zero and is counted in seconds
private double milisecond = 0;
private double second = 0;
private double minute = 0;
private double hour = 0;

public Counter(double s, double m, double h, double ms)
{
milisecond = ms;
second = s;
minute = m;
hour = h;
}
Counter()
{
super();
}
/* Overrided -Indicates that a method declaration is intended to override a method declaration in a superclass */
@Override
public void run()
{
while(true)
{
try
{
sleep(0); // 1000ms = 1 second
milisecond++;

if(milisecond == 1000)
{
milisecond = 0;
second++;
}
if(second == 60)
{
second = 0;
minute++;
}
if(minute == 60)
{
minute = 0;
hour++;
}
if(hour == 24)
{
hour = 0;
}
}catch(Exception e)
{
e.printStackTrace();
}
}

}
public double getMilisecond()
{
return milisecond;
}
public double getSecond()
{
return second;
}
public double getMinute()
{
return minute;
}
public double getHour()
{
return hour;
}

}




I also have a main thread class (declared and utilized in the main) which updates the jLabels.

public class UpdateThread extends Thread
{
@Override
public void run()
{
while(true)
{
try
{
jmilisecondDisplay.setText(""+clockTimer.getMilisecond());

}catch(Exception e)
{
e.printStackTrace();;
}
}
}
}



Ok so I am trying to make two buttons, one which starts the counter (which initially gets displayed in the jLabel).
Ok so I put an action performed on a button which starts the clock thread and one which yields it (pause)

The start works fine but if you click it again there is an error, the yield does not work...


private void jstartButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.start();
}

private void jclearButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
counter.yield();
}


What is a better work around for this situation where i can have a start button and a pause button..

ProgramThis
November 19th, 2009, 08:04 AM
Okay, the first thing to do is read the API on what the methods you are calling do.

The start method will create a lightweight process within the context (JVM) and invoke the run() method, starting the thread. Once the thread is started, you cannot try to start it again.

From the API:
Throws:
IllegalThreadStateException - if the thread was already started.

The yield() method will indeed pause/halt the currently executing thread. However, all it does is yield the thread of execution onto another thread and it places the yielded thread back onto the thread pool to be executed again. It will not create the 'pause' effect you are hoping for.

Why are you performing a sleep(0) in the loop?

The best way to block a thread (i.e. 'pause' the thread) is to use what is called a mutex. When the thread that is the time has the mutex it will perform its counting, when it loses the lock on the mutex (you click the pause button), another thread (the blocking one) will hold the lock on the mutex until you click on resume.