-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
92 lines (69 loc) · 3.03 KB
/
Solution.java
File metadata and controls
92 lines (69 loc) · 3.03 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
//A woman, when she gives birth, has sorrow, because her time has come. But when she has delivered the child, she doesn't remember the anguish any more, for the joy that a human being is born into the world. (John 16:21)
package com.javarush.task.task16.task1621;
/*
Thread.currentThread - всегда возвращает текущую нить
*/
public class Solution {
static int count = 5;
public static void main(String[] args) {
NameOfDifferentThreads tt = new NameOfDifferentThreads();
tt.start();
for (int i = 0; i < count; i++) {
tt.printMsg();
}
}
public static class NameOfDifferentThreads extends Thread {
public void run() {
for (int i = 0; i < count; i++) {
printMsg();
}
}
public void printMsg() {
Thread t = Thread.currentThread();//присвой переменной t текущую нить
String name = t.getName();
System.out.println("name=" + name);
try{//add sleep here - добавь sleep тут
Thread.sleep(1);
}catch (Exception e) {e.printStackTrace();}
}
}
}
/*
Thread.currentThread - всегда возвращает текущую нить
1. В методе printMsg присвой переменной t текущую нить.
2. В методе printMsg после всех действий поставь задержку в 1 миллисекунду.
Требования:
1. Метод printMsg должен получать текущую нить с помощью Thread.currentThread.
2. Метод printMsg должен должен усыплять нить на 1 миллисекунду.
3. Метод printMsg должен вызывать метод getName у текущей нити.
4. Метод main должен вызвать метод printMsg у объекта типа NameOfDifferentThreads 5 раз.
5. Метод run должен вызвать метод printMsg 5 раз.
6. Метод printMsg у объекта типа NameOfDifferentThreads суммарно должен быть вызван 10 раз.
package com.javarush.task.task16.task1621;
*
Thread.currentThread - всегда возвращает текущую нить
*
public class Solution {
static int count = 5;
public static void main(String[] args) {
NameOfDifferentThreads tt = new NameOfDifferentThreads();
tt.start();
for (int i = 0; i < count; i++) {
tt.printMsg();
}
}
public static class NameOfDifferentThreads extends Thread {
public void run() {
for (int i = 0; i < count; i++) {
printMsg();
}
}
public void printMsg() {
Thread t = null;//присвой переменной t текущую нить
String name = t.getName();
System.out.println("name=" + name);
//add sleep here - добавь sleep тут
}
}
}
*/