-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadDemo.java
40 lines (29 loc) · 832 Bytes
/
ThreadDemo.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
package com.codecafe.concurrency.thread.basics.designathread;
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 1000; i++)
System.out.print("T");
}
}
class MyTask implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++)
System.out.print("-");
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread th1 = new MyThread();
th1.start(); // submit the thread for execution
// MyTask is runnable but it is not a thread
MyTask task = new MyTask();
// in order to run in inside a thread, we need to create a Thread object
// and submit this task object for execution
Thread th2 = new Thread(task);
th2.start();
for (int i = 0; i < 1000; i++)
System.out.print("M");
}
}