-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskThreadTest.java
65 lines (52 loc) · 1.06 KB
/
TaskThreadTest.java
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
public class TaskThreadTest
{
//https://www.w3schools.com/java/java_interface.asp
public static void main(String[] args)
{
//Create tasks
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);
//Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
//Start threads
thread1.start();
thread2.start();
thread3.start();
}
}
//The type PrintChar must implement the inherited abstract method Runnable.run()
class PrintChar implements Runnable
{
private char charToPrint;
private int count;
public PrintChar(char p, int c)
{
charToPrint = p;
count = c;
}
@Override
public void run()
{
for(int i = 0; i < count; i++)
{
System.out.println(charToPrint);
}
}
}
class PrintNum implements Runnable
{
private int count;
public PrintNum(int print100)
{
count = print100;
}
@Override
public void run()
{
for(int i = 0; i < count; i++)
System.out.println(" " + i);
}
}