要求:实现一个线程输出数字一个线程输出字母交替执行
方式一、通过LockSupport方式
static Thread t_char=null;
static Thread t_num=null;
public static void main(String[] args) {
char []num="123456789".toCharArray();
char []chars="abcdefghi".toCharArray();
way1(num,chars);
}
/**
* 方案一,通过LockSupport
* @param num
* @param chars
*/
public static void way1(char[]num,char []chars){
t_num=new Thread(()->{
for(char c:num){
System.out.print(c);
LockSupport.unpark(t_char);
LockSupport.park();
}
});
t_char=new Thread(()->{
for(char c:chars){
LockSupport.park();
System.out.print(c);
LockSupport.unpark(t_num);
}
});
t_char.start();
t_num.start();
}
方式二、通过Synchronized关键字、Object的notify()和wait()方法
static Thread t_char=null;
static Thread t_num=null;
public static void main(String[] args) {
char []num="123456789".toCharArray();
char []chars="abcdefghi".toCharArray();
way2(num,chars);
}
/**
* 方案二,通过Object的wait()和notify()方法
* @param num
* @param chars
*/
public static void way2(char[]num,char []chars){
Object lock= new Object();
t_num=new Thread(()->{
synchronized (lock){
for(char c:num){
System.out.print(c);
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t_char=new Thread(()->{
synchronized (lock){
for(char c:chars){
try {
lock.wait();//让出锁
lock.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print(c);
}
}
});
t_char.start();
t_num.start();
}
方式三、通过ReentrantLock和Condition
static Thread t_char = null;
static Thread t_num = null;
public static void main(String[] args) {
char[] num = "123456789".toCharArray();
char[] chars = "abcdefghi".toCharArray();
way3(num, chars);
}
/**
* 方案三,通过ReentrantLock和Condition
* @param num
* @param chars
*/
public static void way3(char[] num, char[] chars) {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
t_num = new Thread(() -> {
for (char c : num) {
System.out.print(c);
try {
lock.lock();
condition.signal();
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
});
t_char = new Thread(() -> {
for (char c : chars) {
try {
lock.lock();
condition.await();
condition.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
System.out.print(c);
}
});
t_char.start();
t_num.start();
}
方案四、通过LinkedTransferQueue
static Thread t_char=null;
static Thread t_num=null;
public static void main(String[] args) {
char []num="123456789".toCharArray();
char []chars="abcdefghi".toCharArray();
way4(num,chars);
}
/**
* 方案四,通过LinkedTransferQueue
* @param num
* @param chars
*/
public static void way4(char[]num,char []chars){
LinkedTransferQueue queue=new LinkedTransferQueue<Character>();
t_num=new Thread(()->{
for(char c:num){
try {
//往queue中放
queue.transfer(c);
//等待别的线程拿,如果不拿走,就等待
System.out.print(queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t_char=new Thread(()->{
for(char c:chars){
try {
//从queue中取,如果没有则等待
System.out.print(queue.take());
queue.transfer(c);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t_char.start();
t_num.start();
}