Java day23

编程入门 行业动态 更新时间:2024-10-27 00:35:23

<a href=https://www.elefans.com/category/jswz/34/1770091.html style=Java day23"/>

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

本文发布于:2023-07-03 10:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1005263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!