-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread.java
More file actions
62 lines (53 loc) · 1.4 KB
/
MyThread.java
File metadata and controls
62 lines (53 loc) · 1.4 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
package java_core_adv;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyThread extends Thread {
boolean flag = true;
@Override
public void run() {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("output/output.txt", false);
while (flag) {
Random random = new Random();
int num = random.nextInt(101);
String line = num + "\n";
byte[] b = line.getBytes();
fos.write(b);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyThread thread = new MyThread();
thread.start();
while (true) {
String command = sc.nextLine();
if (command.equals("stop")) {
thread.flag = false;
break;
}
}
sc.close();
}
}