1.LockSupport
package com.example.demo;
import java.util.concurrent.locks.LockSupport;
/**
* 交替打印1A2B3C......26Z
*/
public class Thread_LockSupport extends Thread {
static Thread t1 = null, t2 = null;
@Override
public void run() {
}
public static void main(String[] args) throws Exception{
t1 = new Thread(()->{
for(int i = 1;i<=26;i++){
System.out.print(i);
LockSupport.unpark(t2);
LockSupport.park();
}
}, "t1");
t2 = new Thread(()->{
for(char p = 'A'; p <= 'Z'; p++) {
LockSupport.park();
System.out.print(p);
LockSupport.unpark(t1);
}
},"'t2");
t1.start();
t2.start();
}
}
2.BlockingQueue
package com.example.demo;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
public class Thread_TransferQueue extends Thread {
private static BlockingDeque<String> bq1 = new LinkedBlockingDeque<String>(1);
private static BlockingDeque<String> bq2 = new LinkedBlockingDeque<String>(1);
static Thread t1 = null, t2 = null;
@Override
public void run() {
}
public static void main(String[] args){
t1 = new Thread(()->{
for(int i = 1;i<=26;i++){
try {
bq1.put(i+"");
System.out.print(bq2.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
t2 = new Thread(()->{
for(char p = 'A'; p <= 'Z'; p++) {
try {
System.out.print(bq1.take());
bq2.put(p+"");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"'t2");
t1.start();
t2.start();
}
}
3.synchronized wait notify
package com.example.demo;
public class Thread_sync extends Thread {
static Thread t1 = null, t2 = null;
private volatile static Object obj = new Object();
@Override
public void run() {
}
public static void main(String[] args){
t1 = new Thread(()->{
for(int i = 1;i<=7;i++){
synchronized (obj){
System.out.print(i);
try {
obj.notify();
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
obj.notify();//必须要有,不然总会有一个线程最后处于阻塞状态
}
}
}, "t1");
t2 = new Thread(()->{
for(char p = 'A'; p <= 'G'; p++) {
synchronized (obj){
System.out.print(p+"");
try {
obj.notify();
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
obj.notify();//必须要有,不然总会有一个线程最后处于阻塞状态
}
}
},"'t2");
t1.start();
t2.start();
}
}
4.Lock Condition
package com.example.demo;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 可保证先打印数字
*/
public class Thread_LockCondition2 extends Thread {
static Thread t1 = null, t2 = null;
static ReentrantLock lock = new ReentrantLock(true);
static Condition condition1 = lock.newCondition();
static Condition condition2 = lock.newCondition();
private static volatile Boolean ifT2Start = false;
@Override
public void run() {
}
public static void main(String[] args){
t1 = new Thread(()->{
try {
lock.lock();
while(!ifT2Start){
for(int i = 1;i<=7;i++){
System.out.print(i);
condition1.signal();
condition2.await();
}
}
condition1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}, "t1");
t2 = new Thread(()->{
try {
lock.lock();
ifT2Start = true;
for(char p = 'A'; p <= 'G'; p++) {
System.out.print(p+"");
condition2.signal();
condition1.await();
}
condition2.signal();
}catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
},"'t2");
t1.start();
t2.start();
}
}
5.TransferQueue
package com.example.demo;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
public class Thread_TransferQueue extends Thread {
private static TransferQueue<String> tq = new LinkedTransferQueue<String>();
static Thread t1 = null, t2 = null;
@Override
public void run() {
}
public static void main(String[] args){
t1 = new Thread(()->{
for(int i = 1;i<=7;i++){
try {
tq.transfer(i+"");
System.out.print(tq.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
t2 = new Thread(()->{
for(char p = 'A'; p <= 'G'; p++) {
try {
System.out.print(tq.take());
tq.transfer(p+"");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"'t2");
t1.start();
t2.start();
}
}
6.自旋
package com.example.demo;
public class Thread_self extends Thread {
static volatile int x = 0;
static Thread t1 = null, t2 = null;
@Override
public void run() {
}
public static void main(String[] args){
t1 = new Thread(()->{
for(int i = 1;i<=7;i++){
while(x!=0){}
System.out.print(i+"");
x = 1;
}
}, "t1");
t2 = new Thread(()->{
for(char p = 'A'; p <= 'G'; p++) {
while(x!=1){}
System.out.print(p+"");
x = 0;
}
},"'t2");
t1.start();
t2.start();
}
}