天天乐学 面试直接让我手撕代码
完全不会呀 那不完蛋~
两个线程,线程1打印A,线程2打印B,两个线程同时并发,要求保证先打印A,后打印B
使用synchronized+ wait、notify
public class Main{
static class Number{
public int num = 1;
Number(){}
}
public static Number number = new Number();
public static void main(String []args) {
Thread a = new Thread(new Runnable(){
@Override
public void run() {
synchronized(number){
try{
//避免假唤醒
while(number.num == 2) number.wait();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("A");
number.num = 2;
number.notify();
}
}
});
Thread b = new Thread(new Runnable(){
@Override
public void run() {
synchronized(number) {
try{
while(number.num == 1) number.wait();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("B");
number.num = 1;
number.notify();
}
}
});
b.start();
try{
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
a.start();
}
}
使用ReetrantLock+Condition
import java.util.concurrent.locks.*;
public class Main{
public static ReentrantLock lock = new ReentrantLock();
public static Condition condition = lock.newCondition();
public static int num = 1;
public static void main(String []args) {
Thread a = new Thread(new Runnable(){
@Override
public void run() {
lock.lock();
try{
while(num == 2) condition.await();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("A");
num = 2;
condition.signal();
lock.unlock();
}
});
Thread b = new Thread(new Runnable(){
@Override
public void run() {
lock.lock();
try{
while(num == 1) condition.await();
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("B");
num = 1;
condition.signal();
lock.unlock();
}
});
b.start();
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
};
a.start();
}
}
两个线程,一个线程打印奇数,一个线程打印偶数,控制先一个奇数后一个偶数这种顺序.使用ReetrantLock+Condition
import java.util.concurrent.locks.*;
public class Main{
public static ReentrantLock lock = new ReentrantLock();
public static Condition condition = lock.newCondition();
public static int num = 2;
public static void main(String []args) {
Thread a = new Thread(new Runnable(){
@Override
public void run() {
try{
for(int i = 2; i <= 10; i=i+2) {
lock.lock();
while(num == 2) condition.await();
System.out.print(i);
num = 2; condition.signal();
lock.unlock();
}
}catch(Exception e) {
e.printStackTrace();
}
}
});
Thread b = new Thread(new Runnable(){
@Override
public void run() {
try{
for(int i = 1; i <= 10; i = i+2){
lock.lock();
while(num == 1) condition.await();
System.out.print(i);
num = 1; condition.signal();
lock.unlock();
}
}catch(Exception e) {
e.printStackTrace();
}
}
});
a.start();
try{
Thread.sleep(10);
}catch(Exception e){
e.printStackTrace();
};
b.start();
}
}