三种同步方式 实现 卖票
同步代码块
public class 同步代码块 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
Thread t = new Thread(ticket,"窗口一");
Thread t2 = new Thread(ticket,"窗口二");
Thread t3 = new Thread(ticket,"窗口三");
Thread t4 = new Thread(ticket,"窗口四");
t.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket implements Runnable{
int ticket = 100;
@Override
public void run() {
while(true) {
synchronized (this) {
if(ticket>0) {
ticket--;
System.out.println(Thread.currentThread().getName()+"卖了一张,还剩"+ticket+"张");
}
}
}
}
}
同步方法
public class 同步方法 {
public static void main(String[] args) {
Ticket2 ticket2 = new Ticket2();
Thread t = new Thread(ticket2,"窗口一");
Thread t2 = new Thread(ticket2,"窗口二");
Thread t3 = new Thread(ticket2,"窗口三");
Thread t4 = new Thread(ticket2,"窗口四");
t.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket2 implements Runnable{
static int ticket2 = 1000;
@Override
public void run() {
// for (int i = 0; i < ticket2; i++) {
// a();
// }
while(true) {
a();
if(ticket2==0) {
break;
}
}
}
public static synchronized void a() {
if(ticket2>0) {
ticket2--;
System.out.println(Thread.currentThread().getName()+"卖了一张,还剩"+ticket2+"张");
}
}
}
Lock显式
public class Lock显式 {
public static void main(String[] args) {
Ticket3 ticket = new Ticket3();
Thread t = new Thread(ticket,"窗口一");
Thread t2 = new Thread(ticket,"窗口二");
Thread t3 = new Thread(ticket,"窗口三");
Thread t4 = new Thread(ticket,"窗口四");
t.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket3 implements Runnable{
int ticket3 = 100;
Lock lock = new ReentrantLock();
@Override
public void run() {
while(true) {
try {
lock.lock();
if(ticket3>0) {
ticket3--;
System.out.println(Thread.currentThread().getName()+"卖出一张,还剩"+ticket3+"张");
}else {
break;
}
} finally {
lock.unlock();
}
}
}
}
2. 三种同步方式 单例
同步代码块
懒汉式类
class LazyInstance {
private LazyInstance() {
}
private static LazyInstance instance = null;
public static LazyInstance getInstance() {
if(instance==null) {
synchronized (LazyInstance.class) {
if(instance==null) {
instance = new LazyInstance();
}
}
}
return instance;
}
}
public class 同步锁代码块 {
public static void main(String[] args) {
MyThread m = new MyThread();
MyThread m2 = new MyThread();
MyThread m3 = new MyThread();
MyThread m4 = new MyThread();
m.start();
m2.start();
m3.start();
m4.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
LazyInstance instance = LazyInstance.getInstance();
System.out.println(instance.hashCode());
}
}
同步方法
public class 同步方法 {
public static void main(String[] args) {
MyThread2 m = new MyThread2();
MyThread2 m2 = new MyThread2();
MyThread2 m3 = new MyThread2();
MyThread2 m4 = new MyThread2();
m.start();
m2.start();
m3.start();
m4.start();
}
}
class MyThread2 extends Thread{
@Override
public void run() {
a();
}
public synchronized void a() {
LazyInstance instance = LazyInstance.getInstance();
System.out.println(instance.hashCode());
}
}
lock显式
public class Lock显式 {
public static void main(String[] args) {
MyThread3 m = new MyThread3();
MyThread3 m2 = new MyThread3();
MyThread3 m3 = new MyThread3();
MyThread3 m4 = new MyThread3();
m.start();
m2.start();
m3.start();
m4.start();
}
}
class MyThread3 extends Thread{
Lock lock = new ReentrantLock();
@Override
public void run() {
try {
lock.lock();
LazyInstance instance = LazyInstance.getInstance();
System.out.println(instance.hashCode());
} finally {
lock.unlock();
}
}
}