这里写自定义目录标题
package com.thread;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SxAbc{
static volatile int anInt=1;
static Thread thread1=new Thread(){
@Override
public void run() {
synchronized (this) {
if (anInt != 1) {
try {
System.out.println("thread1线程开始阻塞");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A");
anInt++;
}
synchronized(thread2) {
thread2.notify();
}
}
};
static Thread thread2=new Thread(){
@Override
public void run() {
synchronized (this) {
if (anInt != 2) {
try {
System.out.println("thread2线程开始阻塞");
this.wait();
System.out.println("thread2线程被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("B");
anInt++;
}
synchronized(thread3) {
thread3.notify();
}
}
};
static Thread thread3=new Thread(){
@Override
public void run() {
synchronized (this) {
if (anInt != 3) {
try {
System.out.println("thread3线程开始阻塞");
this.wait();
System.out.println("thread3线程被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("C");
}
}
};
public static void dxc(){
SxAbc sxAbc=new SxAbc();
sxAbc.thread1.start();
sxAbc.thread2.start();
sxAbc.thread3.start();
}
public static void xcc(){
ExecutorService executorService= Executors.newFixedThreadPool(3);
executorService.execute(new Thread(){
@Override
public void run() {
System.out.println("A");
}
});
executorService.execute(new Thread(){
@Override
public void run() {
System.out.println("B");
}
});
executorService.execute(new Thread(){
@Override
public void run() {
System.out.println("C");
}
});
executorService.shutdown();
}
public static void dxcjoin(){
Thread thread1=new Thread(){
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1:"+Thread.currentThread().getId());
}
};
Thread thread2=new Thread(){
@Override
public void run() {
try {
thread1.join();
Thread.sleep(2000);
System.out.println("thread2:"+Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread3=new Thread(){
@Override
public void run() {
try {
thread2.join();
Thread.sleep(1000);
System.out.println("thread3:"+Thread.currentThread().getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread1.start();
thread2.start();
thread3.start();
}
public static void lockThread(){
Runs runs=new Runs();
Thread thread1=new Thread(new RunA(runs));
Thread thread2=new Thread(new RunB(runs));
Thread thread3=new Thread(new RunC(runs));
thread1.start();
thread2.start();
thread3.start();
}
public static void main(String[] args) {
lockThread();
}
}
class Runs{
private int status = 1;
private ReentrantLock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
public void printA(){
lock.lock();
try {
if (status != 1) {
condition1.await();
}
System.out.print("A");
status=2;
condition2.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printB(){
lock.lock();
try {
if(status!=2){
condition2.await();
}
System.out.print("B");
status=3;
condition3.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void printC(){
lock.lock();
try {
if(status!=3){
condition3.await();
}
System.out.println("C");
status=1;
condition1.signal();
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
class RunA implements Runnable{
private Runs runs;
public RunA(Runs runs){
super();
this.runs=runs;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
runs.printA();
}
}
}
class RunB implements Runnable{
private Runs runs;
public RunB(Runs runs){
super();
this.runs=runs;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
runs.printB();
}
}
}
class RunC implements Runnable{
private Runs runs;
public RunC(Runs runs){
super();
this.runs=runs;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
runs.printC();
}
}
}