S
skoobie
Hi, this is my first post so please excuse any mistakes. I basically
have scoured over the net and have read many books on Multi-threading
and I am struggling to use it for this problem.
The problem is creating a digitalclock (displaying hours, minutes and
seconds) program which uses a different thread for hours, minutes and
seconds.
Using one thread would not be a problem ( I can do that fine) but
synchronising 3 threads with split second timing is proving to be
difficult. So far I can get the seconds to work but am struggling to
incorporate the minutes and hours.
What I have set out to do is to make hours and mins threads start
first and then wait();
The seconds thread sleeps every one second and set new seconds each
time.
if seconds == 0, notify the minutes thread
The minutes thread then updates minutes and should notify hours when
it reaches 0.
I have written good code for setting the seconds and displaying it. I
am just having problems synchronising the 3 threads. Here is a snipet
of my code:
public void start() {
if (threadS == null) {
threadS = new Thread(new Clock("seconds")); //this, the current
object instance
threadS.setPriority(10); // ensures second starts first
threadS.start();
}
if (threadM == null) {
threadM = new Thread(new Clock("minutes")); //this, the current
object instance
threadM.setPriority(5); // ensures second starts first
// threadM.start();
}
if (threadH == null) {
threadH = new Thread(new Clock("hours")); //this, the current
object instance
threadH.setPriority(2); // ensures second starts first
threadH.start();
}
if (startMins == true) { // flag to start thread
System.err.println("Hello");
threadM.start();
}
/* and in the action part of the code Synchronised method belonging to
run(); */
while (threadS == Thread.currentThread()) {
// setting new seconds code
try {
Thread.sleep(addMilliSec);
} catch (InterruptedException e) { }
if (newSeconds == 00)) {
startMins = true;
start(); // without this threadM doesn't start!
notify();
}
System.out.println(getDigitalClock());
}
while (threadM == Thread.currentThread()) {
minute = newMinutes();
try {
wait();
} catch (InterruptedException e) {}
}
// and so fourth..
The minute increments correctly first time but then I get an
IllegalThreadStateException.
I know why this is.. its because I am trying to start a thread that's
already started before. But if I don't put start() in there.. seconds
continue to update but minutes and seconds don't.
What am I missing?
Any guidance to this would be very helpful. his is very frustrating
because I've got working solutions with one thread but have hit a dead
end trying to use 3.
Thanks for reading,
Skoobie
have scoured over the net and have read many books on Multi-threading
and I am struggling to use it for this problem.
The problem is creating a digitalclock (displaying hours, minutes and
seconds) program which uses a different thread for hours, minutes and
seconds.
Using one thread would not be a problem ( I can do that fine) but
synchronising 3 threads with split second timing is proving to be
difficult. So far I can get the seconds to work but am struggling to
incorporate the minutes and hours.
What I have set out to do is to make hours and mins threads start
first and then wait();
The seconds thread sleeps every one second and set new seconds each
time.
if seconds == 0, notify the minutes thread
The minutes thread then updates minutes and should notify hours when
it reaches 0.
I have written good code for setting the seconds and displaying it. I
am just having problems synchronising the 3 threads. Here is a snipet
of my code:
public void start() {
if (threadS == null) {
threadS = new Thread(new Clock("seconds")); //this, the current
object instance
threadS.setPriority(10); // ensures second starts first
threadS.start();
}
if (threadM == null) {
threadM = new Thread(new Clock("minutes")); //this, the current
object instance
threadM.setPriority(5); // ensures second starts first
// threadM.start();
}
if (threadH == null) {
threadH = new Thread(new Clock("hours")); //this, the current
object instance
threadH.setPriority(2); // ensures second starts first
threadH.start();
}
if (startMins == true) { // flag to start thread
System.err.println("Hello");
threadM.start();
}
/* and in the action part of the code Synchronised method belonging to
run(); */
while (threadS == Thread.currentThread()) {
// setting new seconds code
try {
Thread.sleep(addMilliSec);
} catch (InterruptedException e) { }
if (newSeconds == 00)) {
startMins = true;
start(); // without this threadM doesn't start!
notify();
}
System.out.println(getDigitalClock());
}
while (threadM == Thread.currentThread()) {
minute = newMinutes();
try {
wait();
} catch (InterruptedException e) {}
}
// and so fourth..
The minute increments correctly first time but then I get an
IllegalThreadStateException.
I know why this is.. its because I am trying to start a thread that's
already started before. But if I don't put start() in there.. seconds
continue to update but minutes and seconds don't.
What am I missing?
Any guidance to this would be very helpful. his is very frustrating
because I've got working solutions with one thread but have hit a dead
end trying to use 3.
Thanks for reading,
Skoobie