/*
* 共享资源冲突的问题
*/
class SingleThread implements Runnable {
// 共享资源,100张票
private static int ticket = 100;
@Override
public void run() {
while (true) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() +
"售出了第" + ticket + "张票");
ticket -= 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() +
"售罄!!!");
break;
}
}
}
}
public class Demo1 {
public static void main(String[] args) {
Thread thread = new Thread(new SingleThread(),"淘票票");
Thread thread2 = new Thread(new SingleThread(),"猫眼");
Thread thread3 = new Thread(new SingleThread(),"美团");
thread.start();
thread2.start();
thread3.start();
}
}
从上面的例子上可以看到,最后一张票被每一个平台都卖了一次,这样子肯定是不符合实际情况的,这就是本次我们需要解决的问题。
synchronized ("锁") {
//需要同步的代码
}
// 1. 使用同步代码块解决多线程中共享资源冲突的问题
/**
* 自定义售票线程类
*
* @author
*
*/
//同步代码块
class SingleThread1 implements Runnable {
// 共享资源,100张票
private static int ticket = 100;
@Override
public void run() {
while (true) {
synchronized ("锁") {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() +
"售出了第" + ticket + "张票");
ticket -= 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() +
"售罄!!!");
break;
}
}
}
}
}
public class Demo2 {
public static void main(String[] args) {
Thread thread = new Thread(new SingleThread1(), "淘票票");
Thread thread2 = new Thread(new SingleThread1(), "猫眼");
Thread thread3 = new Thread(new SingleThread1(), "美团");
thread.start();
thread2.start();
thread3.start();
}
}
// 2.1 使用静态成员方法解决多线程中共享资源冲突的问题
/**
* 自定义售票线程类
*
* @author
*
*/
//同步方法
class SingleThread3 implements Runnable {
// 共享资源,100张票
private static int ticket = 100;
@Override
public void run() {
while (true) {
sellTicket();
if(ticket <= 0) {
break;
}
}
}
// 静态同步方法
public static synchronized void sellTicket() {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() +
"售出了第" + ticket + "张票");
ticket -= 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() + "售罄!!!");
}
}
}
public class Demo3 {
public static void main(String[] args) {
//使用静态同步方法
Thread thread1 = new Thread(new SingleThread3(), "淘票票");
Thread thread2 = new Thread(new SingleThread3(), "猫眼");
Thread thread3 = new Thread(new SingleThread3(), "美团");
thread1.start();
thread2.start();
thread3.start();
}
}
// 2.2 使用静态成员方法解决多线程中共享资源冲突的问题
/**
* 自定义售票线程类
*
* @author
*
*/
//同步方法
class SingleThread3 implements Runnable {
// 共享资源,100张票
private static int ticket = 100;
@Override
public void run() {
while (true) {
sellTicket();
if(ticket <= 0) {
break;
}
}
}
//非静态同步方法
public synchronized void sellTicket() {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() +
"售出了第" + ticket + "张票");
ticket -= 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() +
"售罄!!!");
}
}
}
public class Demo3 {
public static void main(String[] args) {
//使用非静态同步方法
//使用同一个目标代码对象
SingleThread3 singleThread3 = new SingleThread3();
Thread thread1 = new Thread(singleThread3,"淘票票");
Thread thread2 = new Thread(singleThread3,"猫眼");
Thread thread3 = new Thread(singleThread3,"美团");
}
}
// 使用静态Lock锁解决共享资源冲突的问题
/**
* 自定义售票线程类
*
* @author
*
*/
//lock加锁操作
class SingleThread4 implements Runnable {
// 共享资源,100张票
private static int ticket = 100;
//创建锁对象,这里使用的是静态的方式,锁对象是唯一的
static Lock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
//加锁
lock.lock();
if (ticket > 0) {
System.out.println(Thread.currentThread().getName() +
"售出了第" + ticket + "张票");
ticket -= 1;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() +
"售罄!!!");
break;
}
lock.unlock();
}
}
}
public class Demo4 {
public static void main(String[] args) {
Thread thread = new Thread(new SingleThread4(), "淘票票");
Thread thread2 = new Thread(new SingleThread4(), "猫眼");
Thread thread3 = new Thread(new SingleThread4(), "美团");
thread.start();
thread2.start();
thread3.start();
}
}