Java day23
java day23
同步方法
使用synchronized修飾的方法
package Demo01;
public class Demo01Ticket {
public static void main(String[] args) {
RunnableImpl run = new RunnableImpl();
Thread t0 = new Thread(run);
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t0.start();
t1.start();
t2.start();
}
}
Lock鎖
package Demo02;
public class Demo01Ticket {
public static void main(String[] args) {
RunnableImpl run = new RunnableImpl();
Thread t0 = new Thread(run);
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t0.start();
t1.start();
t2.start();
}
}
喚醒案例
package Demo03;
public class Demo03WatiAndNotify {
public static void main(String[] args) {
Object obj = new Object();
new Thread() {
@Override
public void run() {
while(true) {
synchronized(obj) {
System.out.println(“消費者:告知老板要的包子的種類”);
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(“包子已經做好了,開吃”);
System.out.println("====================");
}
}
}
}
2020080605024
總結
以上是生活随笔為你收集整理的Java day23的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机语言的魅力,四年级语文下册《语言的
- 下一篇: VC 常见的108个问题[转]