哎呀,被我抓到了吧!
那你就别走了~
文章目录
- JavaEE & wait and notify & 线程状态总结
- 1. wait and notify
- 1.1 应用场景
- 1.2 wait与notify配合解决问题
- 1.2.1 wait和notify的使用
- 1.2.2 wait和notify代码规范
- 1.2.3 补充
- 2. 线程状态总结
- 2.1 NEW
- 2.2 RUNNABLE
- 2.3 TERMINATED
- 2.4 TIMED_WAITING
- 2.5 BLOCKED
- 2.6 WAITING
根据 【适用场景:::我们的初心】 去选择阻塞方式~
public class Test {
public static void main(String[] args) throws InterruptedException {
Object object = new Object();
Thread thread1 = new Thread(() -> {
while(true) {
System.out.println("wait 前");
synchronized (object) {
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("wait 后");
}
});
Thread thread2 = new Thread(() -> {
System.out.println("notify 开始");
synchronized (object) {
object.notify();
}
System.out.println("notify 结束");
});
thread1.start();
thread2.start();
}
}
很多方法都能引起这种效果,但是初心不同,所以,要看具体想法,使用特定的方式~
sleep,join好像也能被中断唤醒,也有时间限制,这样就跟wait差不多了咯~
目前的场景只是为了体现wait和notify的使用方式
你可能没能体会到,他们的配合的在这里对任务的针对性~
没有关系,后面我会写几篇博客,研究 Java多线程的一些案例
NEW 线程还没被创建出来,只是存在这个线程对象~
public class TestState {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
});
System.out.println(thread1.getState());
thread1.start();
}
}
RUNNABLE 运行/就绪状态
public class TestState {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
});
thread1.start();
System.out.println(thread1.getState());
}
}
TERMINATED 系统中的线程已经执行完毕~
由于线程调度的随机性,所以2.2代码是可能出现TERMINATED状态的,如下图:
所以我让main线程等一会儿~
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
});
thread1.start();
Thread.sleep(10);
System.out.println(thread1.getState());
}
}
TIMED_WAITING
public class TestState {
public static Thread t;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
System.out.println(t.getState());
});
thread1.start();
t = Thread.currentThread();
thread1.join(1000);
}
}
public class TestState {
public static Thread t;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
System.out.println(t.getState());
});
thread1.start();
t = Thread.currentThread();
Thread.sleep(1000);
}
}
public class TestState {
public static Thread t;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
System.out.println("好耶 ^ V ^");
System.out.println(t.getState());
});
thread1.start();
t = Thread.currentThread();
thread1.join();
}
}
BLOCKED
class Counter {
private int count = 0;
public void add() {
synchronized (Counter.class) {
count++;
}
}
public int get() {
return count;
}
}
public class TestState {
public static Thread t;
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 5000; i++) {
synchronized (counter) {
counter.add();
System.out.println(t.getState());
}
}
});
thread1.start();
t = Thread.currentThread();
for (int i = 0; i < 5000; i++) {
synchronized (counter) {
counter.add();
}
}
}
}
public class TestState {
public static void main(String[] args) throws InterruptedException {
Object object = new Object();
Thread thread1 = new Thread(() -> {
while(true) {
System.out.println("wait 前");
synchronized (object) {
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("wait 后");
}
});
Thread thread2 = new Thread(() -> {
System.out.println("notify 开始");
System.out.println(thread1.getState());
synchronized (object) {
System.out.println(thread1.getState());
object.notify();
}
System.out.println("notify 结束");
System.out.println(thread1.getState());
});
thread1.start();
thread2.start();
}
}
文章到此结束!谢谢观看 !
可以叫我 小马,我可能写的不好或者有错误,但是一起加油鸭!后续会有几篇关联博客,单例模式,阻塞队列,工厂模式,计时器,线程池等等…
敬请期待吧~