-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskThread.java
More file actions
44 lines (42 loc) · 1.07 KB
/
Copy pathTaskThread.java
File metadata and controls
44 lines (42 loc) · 1.07 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
import java.util.*;
import java.util.concurrent.locks.*;
public class TaskThread{
private static HashSet<Integer> hs=new HashSet<Integer>();
private static Lock lock =new ReentrantLock();
public static void main(String[] args){
Runnable task1=new AddData();
Runnable task2=new ShowData();
Thread thread1=new Thread(task1);
Thread thread2=new Thread(task2);
thread1.start();
thread2.start();
}
static class AddData implements Runnable{
public void run(){
Timer timer=new Timer();
timer.schedule(new mytask(),0,1000);
}
class mytask extends TimerTask{
public void run(){
lock.lock();
hs.add((int)(Math.random()*100));
lock.unlock();
}
}
}
static class ShowData implements Runnable{
public void run(){
Timer timer=new Timer();
timer.schedule(new mytask2(),0,1000);
}
class mytask2 extends TimerTask{
public void run(){
lock.lock();
Iterator<Integer> iterator=hs.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
lock.unlock();
}
}
}
}