-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeThreads.java
More file actions
69 lines (58 loc) · 1.67 KB
/
threeThreads.java
File metadata and controls
69 lines (58 loc) · 1.67 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
package test;
import java.util.ArrayList;
import java.util.List;
public class threeThreads
{
public static byte countThreads = 3;
static List<Thread> threads = new ArrayList<Thread>(countThreads);
public static void main(String[] args) throws InterruptedException
{
initThreadsAndStart();
Thread.sleep(3000);
ourInterruptMethod();
}
public static void ourInterruptMethod()
{
for (int i = 0; i < countThreads; i++)
{
threads.get(i).interrupt();
}
}
private static void initThreadsAndStart()
{
Water water = new Water("water");
for (int i = 0; i < countThreads; i++)
{
threads.add(new Thread(water, "#" + i));
}
for (int i = 0; i < countThreads; i++)
{
threads.get(i).start();
}
}
public static class Water implements Runnable
{
private String commonResource;
public Water(String commonResource)
{
this.commonResource = commonResource;
}
public void run()
{
//fix 2 variables - исправь 2 переменных
boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
String threadName = Thread.currentThread().getName();
try
{
while (!isCurrentThreadInterrupted)
{
System.out.println("Объект " + commonResource + ", нить " + threadName);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
}
}
}