package com.test2;
public class SalesTicketTask implements Runnable{
//卖票20张
private int ticket=20;
@Override
public void run() {
while(ticket>0){
synchronized (this){
//double check 实现同步 避免超卖
if(ticket>0){
try {
//线程休眠
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票一张,还剩"+(--ticket));
}
}
}
}
}
package com.test2;
public class Test {
public static void main(String[] args) {
//创建售票任务
SalesTicketTask salesTicketTask=new SalesTicketTask();
new Thread(salesTicketTask,"张三").start();
new Thread(salesTicketTask,"李四").start();
new Thread(salesTicketTask,"王五").start();
}
}
package com.test3;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SalesTicketTask implements Runnable{
//卖票20张
private int ticket=20;
//创建ReentrantLock锁对象
private Lock lock=new ReentrantLock();
@Override
public void run() {
while(ticket>0){
lock.lock();
try {
//double check 实现同步 避免超卖
if(ticket>0){
try {
//线程休眠
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖票一张,还剩"+(--ticket));
}
}finally {
lock.unlock();
}
}
}
}
package com.test3;
public class Test {
public static void main(String[] args) {
SalesTicketTask salesTicketTask=new SalesTicketTask();
new Thread(salesTicketTask,"张三").start();
new Thread(salesTicketTask,"李四").start();
new Thread(salesTicketTask,"王五").start();
}
}
package com.test4;
public class Test {
public static void main(String[] args) {
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("aaaaa");
try {
//通过休眠来控制线程执行次序
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("bbbbb");
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("ccccc");
try {
//通过休眠来控制线程执行次序
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ddddd");
}
});
t1.start();
t2.start();
}
}
package com.test4;
public class Test1 {
public static void main(String[] args) throws InterruptedException{
//1.在没有同步代码块的情况下,使用sleep之后,当前线程直接结束对cpu的占用。此时其他线程就有机会获取到cpu资源
//2.在有同步代码块的情况下,使用sleep之后,当前线程结束对cpu的占用。
// 但是因为当前线程依旧拿着锁,所以其他线程没有办法执行。
// 只有当前线程把锁释放之后,其他线程才有机会执行
//定义锁对象
Object lockObj=new Object();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockObj){
System.out.println("aaaaa");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("bbbbb");
}
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockObj){
System.out.println("ccccc");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ddddd");
}
}
});
t1.start();
t2.start();
}
}
package com.test4;
public class Test2 {
public static void main(String[] args) throws InterruptedException{
//定义锁对象
Object lockObj=new Object();
Thread t1=new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockObj){
System.out.println("aaaaa");
try {
//调用锁对象的wait方法,来让线程等待
lockObj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("bbbbb");
}
}
});
Thread t2=new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockObj){
System.out.println("ccccc");
//通过notify来唤醒其他线程:让其他线程等着当前线程执行完成,然后其他线程再去执行
lockObj.notify();
System.out.println("ddddd");
}
}
});
t1.start();
t2.start();
}
}