-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyRunnable.java
More file actions
69 lines (51 loc) · 1.98 KB
/
MyRunnable.java
File metadata and controls
69 lines (51 loc) · 1.98 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
/**
* Created by Phil on 8/20/2015.
*/
public class MyRunnable implements Runnable {
ConcurrencyObject obj;
public MyRunnable(ConcurrencyObject obj) {
this.obj = obj;
}
@Override
public void run() {
long startTime = System.currentTimeMillis();
// System.out.println("Thread " + Thread.currentThread().getName() + " started at " + startTime);
try {
System.out.println(Thread.currentThread().getName() + " value: " + obj.getNextValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
/* try {
Thread.sleep(5000);
}catch(InterruptedException e){
long interruptTime = System.currentTimeMillis();
System.out.println("Thread " + Thread.currentThread().getName() + " interrupted at " + interruptTime);
System.out.println("Thread " + Thread.currentThread().getName() + " difference of " + (interruptTime - startTime));
}*/
// long finishTime = System.currentTimeMillis();
//System.out.println("Thread " + Thread.currentThread().getName() + " finished at " + finishTime);
//System.out.println("Thread " + Thread.currentThread().getName() + " difference of " + (finishTime - startTime));
}
public static void main(String[] args) {
Runnable runnable = new MyRunnable(new ConcurrencyObject());
Thread thread1 = new Thread(runnable);
thread1.setName("Thread 1");
Thread thread2 = new Thread(runnable);
thread2.setName("Thread 2");
thread1.start();
/* try {
thread1.join();
} catch (InterruptedException e) {
System.out.println("Shit!");
}*/
thread2.start();
/* try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
System.out.println("Main thread interrupted?");
}
// thread2.interrupt();*/
System.out.println("Main Thread!");
}
}