Skip to content

Commit f7bd433

Browse files
committed
update
1 parent 3dcd149 commit f7bd433

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

src/java/Thread/LifeCycle/LifeCycle

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1. New
2+
Thread ở trạng thái new nếu bạn tạo một thể hiện của lớp Thread nhưng trước khi gọi phương thức start().
3+
4+
2. Runnable
5+
Thread ở trạng thái runnable sau khi gọi phương thức start(), nhưng trình lên lịch (scheduler) của thread đã không chọn nó là thread đang chạy.
6+
7+
3. Running
8+
Thread ở trạng thái running nếu trình lên lịch của thread đã chọn nó.
9+
10+
4. Non-Runnable (Blocked)
11+
Đây là trạng thái khi thread vẫn còn sống, nhưng hiện tại không được chọn để chạy.
12+
13+
5. Terminated
14+
Một thread ở trong trạng thái terminated hoặc dead khi phương thức run() của nó bị thoát.

src/java/Thread/LifeCycle/img.png

126 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Thread.ThreadTestDemo;
2+
3+
public class TestThread extends Thread {
4+
private String threadName;
5+
6+
public TestThread(String threadName) {
7+
super();
8+
this.threadName = threadName;
9+
}
10+
11+
public String getThreadName() {
12+
return threadName;
13+
}
14+
15+
public void setThreadName(String threadName) {
16+
this.threadName = threadName;
17+
}
18+
19+
@Override
20+
public void run() {
21+
for(int i = 0; i < 10; i++) {
22+
System.out.println(this.getThreadName() + " " + i);
23+
try {
24+
Thread.sleep(1000);
25+
} catch (InterruptedException e) {
26+
e.printStackTrace();
27+
}
28+
}
29+
30+
}
31+
32+
33+
public static void main(String[] args) {
34+
TestThread t1 = new TestThread("a");
35+
TestThread t2 = new TestThread("b");
36+
37+
t1.start();
38+
t2.start();
39+
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Thread.ThreadTestDemo;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
public class TestThread2 implements Runnable {
7+
8+
private final Lock lock = new ReentrantLock();
9+
10+
private int tong;
11+
12+
public TestThread2() {
13+
this.tong = 1000;
14+
}
15+
16+
public void rutTien() throws InterruptedException {
17+
// cách 1:
18+
synchronized (this) {
19+
if (tong > 0) {
20+
Thread.sleep(1000);
21+
tong = tong - 1000;
22+
System.out.println(tong);
23+
} else {
24+
System.out.println("Không còn tiền");
25+
}
26+
}
27+
28+
29+
// cách 2:
30+
// lock.lock();
31+
// try {
32+
// if (tong > 0) {
33+
// Thread.sleep(1000);
34+
// tong = tong - 1000;
35+
// System.out.println(tong);
36+
// } else {
37+
// System.out.println("Không còn tiền");
38+
// }
39+
// } finally {
40+
// lock.unlock();
41+
// }
42+
43+
}
44+
45+
@Override
46+
public void run() {
47+
try {
48+
rutTien();
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
TestThread2 t = new TestThread2();
56+
57+
Thread t1 = new Thread(t);
58+
Thread t2 = new Thread(t);
59+
t1.start();
60+
t2.start();
61+
62+
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Thread.ThreadTestDemo;
2+
3+
class Account implements Runnable {
4+
private int balance = 1000;
5+
6+
@Override
7+
public void run() {
8+
withDraw();
9+
}
10+
11+
private void withDraw() {
12+
try {
13+
balance -= 1000;
14+
15+
System.out.println(Thread.currentThread().getName());
16+
Thread.sleep(3000);
17+
18+
if (balance < 0) {
19+
System.out.println("Het tien");
20+
} else {
21+
System.out.println("OKE");
22+
}
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
}
28+
29+
public class TestThread3 {
30+
public static void main(String[] args) {
31+
Account account = new Account();
32+
33+
Thread t1 = new Thread(account);
34+
Thread t2 = new Thread(account);
35+
Thread t3 = new Thread(account);
36+
37+
t1.start();
38+
t2.start();
39+
t3.start();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)