多线程模拟火车票售卖
package org.westos.多线程博客练习;
/*
* 多线程实现卖火车票模拟
* */
public class Text1 {
public static void main(String[] args) {
//创建ThicksThread对象
ThicksThread1 tt = new ThicksThread1();
//创建Thread对象
Thread t1 = new Thread(tt,"窗口1");
Thread t2 = new Thread(tt,"窗口2");
Thread t3 = new Thread(tt,"窗口3");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
package org.westos.多线程博客练习;
public class ThicksThread1 implements Runnable {
//有100张票
private static int num = 100;
//创建锁对象
S s = new S();
@Override
public void run() {
while(true) {
//只有一个s,保证一次只有一个Thread对象进入线程
synchronized(s) {//synchronized同步线程,该线程执行完毕后才可以被其他线程抢占
//所用同步代码都应该写在synchronnized内,保证线程的安全
if(num>0) {
//模拟网络延迟,线程睡眠0.1秒
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
num--;
System.out.println(Thread.currentThread().getName()+"卖出一张票"+"还剩余"+num+"张票");
}
}
}
}
}
//锁可以是任意的类
class S{
}
使用Lock类模拟
package org.westos.多线程博客练习;
public class Text2 {
public static void main(String[] args) {
//创建ThicksThread对象
ThicksThread2 tt = new ThicksThread2();
//创建Thread对象
Thread t1 = new Thread(tt,"窗口1");
Thread t2 = new Thread(tt,"窗口2");
Thread t3 = new Thread(tt,"窗口3");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
package org.westos.多线程博客练习;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//多线程的锁对象Lock类;
public class ThicksThread2 implements Runnable{
private static int num = 100;
//采用Lock子实现类ReentrantLock
private Lock lock = new ReentrantLock();
@Override
public void run() {
while(true) {
//同步代码块就像是一扇门,我们在这里使用Lock.lock()给门上锁,防止正在执行的线程被其他线程抢占
lock.lock();
if(num>0) {
try {
Thread.sleep(100);
num--;
System.out.println(Thread.currentThread().getName()+"卖出一张票"+"还剩余"+num+"张票");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//给门开锁,允许所有线程再次开始抢占资源
lock.unlock();
}
}
}
/*
* 同步方法:非静态锁对象为该类的对象this
* 静态的锁对象为类名.java
* */
/*
* 线程安全问题:
* 将ArrayList转换为线程安全的集合类,通过使用Collections.synchronizedlist(list<T> list)方法
* 该方法将返回一个list集合
* */
多线程中存在的死锁
package org.westos.多线程博客练习;
/*
* 死锁:
* synchronized解决了多线程的安全问题,但还是存在以下不足:
* 执行效率问题;
* 会出现死锁;
* */
public class Text3 {
public static void main(String[] args) {
DieThread dt1 = new DieThread(false);
DieThread dt2 = new DieThread(true);
Thread t1 = new Thread(dt1);
Thread t2 = new Thread(dt2);
t1.start();
t2.start();
}
}
package org.westos.多线程博客练习;
public class MyLock {
//自定义锁对象
public static final Object obja = new Object();
public static final Object objb = new Object();
}
package org.westos.多线程博客练习;
//死锁
public class DieThread implements Runnable {
private boolean flag;
public DieThread(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
if(flag) {
synchronized (MyLock.obja) {
System.out.println("这是obja锁");
synchronized (MyLock.objb) {
System.out.println("这是objb锁");
}
}
}else {
synchronized (MyLock.objb) {
System.out.println("这是objb锁");
synchronized (MyLock.obja) {
System.out.println("这是obja锁");
}
}
}
}
}
/**
* 这个死锁将输出3中状态
* 1:这是objb锁
这是objb锁
这是obja锁
这是obja锁
这是objb锁
2:这是objb锁
这是obja锁
3:这是obja锁
这是objb锁
*/