-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerThread.java
More file actions
122 lines (100 loc) · 2.1 KB
/
TimerThread.java
File metadata and controls
122 lines (100 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.ub.thread;
public class TimerThread {
Thread secondThread;
Thread minuteThread;
Thread hourThread;
SecondThread sec = new SecondThread();
MinuteThread min = new MinuteThread();
HourThread hr = new HourThread();
public TimerThread() {
// Three worker thread
// How to assign work to worker thread ?
// How to combine the result ?
// How to know that thread finished their respective work?
secondThread = new Thread(sec,"second thread");
minuteThread = new Thread(min,"Minute thread");
hourThread = new Thread(hr,"hour thread");
secondThread.start();
minuteThread.start();
hourThread.start();
}
public void startTimer() {
while(true) {
System.out.println("");
System.out.println(hr.getHour()+":"+ min.getMin() +":"+ sec.getSecond() );
try {
Thread.sleep(900);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void stopTimer() {
}
public void pauseTimer() {
}
class SecondThread implements Runnable {
private int second = 0;
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
second++;
if(second == 61) {
second = 0;
}
}
}
public int getSecond() {
return second;
}
}
class MinuteThread implements Runnable {
private int min = 0;
@Override
public void run() {
while(true) {
try {
Thread.sleep(60*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
min++;
if(min == 61) {
min = 0;
}
}
}
public int getMin() {
return min;
}
}
class HourThread implements Runnable {
private int hour = 0;
@Override
public void run() {
while(true) {
try {
Thread.sleep(60*60*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hour++;
if(hour == 13) {
hour = 1;
}
}
}
public int getHour() {
return hour;
}
}
}