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