1 public class TaskThreadDemo {
2 public static void main(String[] args) {
3
4 Runnable printA = new PrintChar('a', 100);
5 Runnable printB = new PrintChar('b', 100);
6 Runnable print100 = new PrintNum(100);
7
8
9 Thread thread1 = new Thread(printA);
10 Thread thread2 = new Thread(printB);
11 Thread thread3 = new Thread(print100);
12
13
14 thread1.start();
15 thread2.start();
16 thread3.start();
17 }
18 }
19
20
21 class PrintChar implements Runnable {
22 private char charToPrint;
23 private int times;
24
25
28 public PrintChar(char c, int t) {
29 charToPrint = c;
30 times = t;
31 }
32
33 @Override
36 public void run() {
37 for (int i = 0; i < times; i++) {
38 System.out.print(charToPrint);
39 }
40 }
41 }
42
43
44 class PrintNum implements Runnable {
45 private int lastNum;
46
47
48 public PrintNum(int n) {
49 lastNum = n;
50 }
51
52 @Override
53 public void run() {
54 for (int i = 1; i <= lastNum; i++) {
55 System.out.print(" " + i);
56 }
57 }
58 }