-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread2.java
More file actions
75 lines (65 loc) · 1.88 KB
/
Copy pathMyThread2.java
File metadata and controls
75 lines (65 loc) · 1.88 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
package java_core_adv;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import java_core_basic.QuickSort;
public class MyThread2 extends Thread {
boolean flag = true;
int distance;
public MyThread2(int distance) {
this.distance = distance;
}
@Override
public void run() {
int count = 1;
while (flag) {
if (count >= distance * 60) {
flag = false;
}
Random random = new Random();
int num = random.nextInt(101);
System.out.println(num);
try {
Thread.sleep(distance * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count += distance;
}
System.out.println("done");
}
public static void main(String[] args) {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
reader = new FileReader("config/MyThread2Config.txt");
bufferedReader = new BufferedReader(reader);
String line = bufferedReader.readLine();
int input = Integer.parseInt(line.trim());
System.out.println("Chương trình kết thúc sau " + input + " phút:");
MyThread2 thread2 = new MyThread2(input);
thread2.start();
} catch (FileNotFoundException ex) {
Logger.getLogger(QuickSort.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(QuickSort.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(QuickSort.class.getName()).log(Level.SEVERE, null, ex);
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(QuickSort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}