-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathApp.java
More file actions
42 lines (36 loc) · 1.65 KB
/
Copy pathApp.java
File metadata and controls
42 lines (36 loc) · 1.65 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
public class App extends Thread {
public static void main(String[] args) throws Exception {
//Tạo đối tượng mThread
MultithreadWithThread mThread = new MultithreadWithThread();
mThread.start(); //Bắt đầu khởi chạy thread
//Luồng được chạy bằng cách chuyển một implements của lớp tới constructor của thread
//và gọi phương thức start của luồng
MultithreadWithRunnable mRunnable = new MultithreadWithRunnable();
Thread thread = new Thread(mRunnable);
thread.start();
//Sử dụng sleep method
TestThread test1 = new TestThread();
System.out.println("Name of test1: " +test1.getName()); //Lấy tên
System.out.println("Id of test2: " +test1.getId()); //Lấy id
test1.start();
//Thử start test1 cùng một lúc
//test1.start();
//thêm join
try {
//test1.join(); //đợi luồng 1 chết rồi mới chạy các luồng tiếp theo
test1.join(1500); //khi test1 hoàn thành nhiệm vụ trong 1500 mili giây
//thì test2 và test3 bắt đầu thực hiện
} catch (Exception e) {
e.printStackTrace();
}
TestThread test2 = new TestThread();
System.out.println("Name of test2: " +test2.getName());
TestThread test3 = new TestThread();
System.out.println("Name of test3: " +test3.getName());
test2.start();
test3.start();
//đổi tên test1
test1.setName("Test thread 1");
System.out.println("Name of test1: " +test1.getName());
}
}