为什么80%的码农都做不了架构师?>>>
多线程面试时,经常会考到一个用3个线程循环打印abcabcabc的问题 现用两种方法做了一下,也看了网上的其他 人的思路,大差不差
一个对象,一个状态位控制,代码如下:
package com.myTread.abcabc.my;
import java.util.concurrent.atomic.AtomicInteger;
public class Print implements Runnable {
//一个对象锁
private Object lock;
//一个线程标志
private int flag;
//线程状态
private AtomicInteger number;
//下一个线程的状态
private int next;
public Print(Object lock, int flag, AtomicInteger number, int next) {
this.lock = lock;
this.flag = flag;
this.number = number;
this.next = next;
}
@Override
public void run() {
synchronized(lock){
for (int i = 0;i<10;i++){
while(number.intValue() != flag ){
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName());
number.set(next);
lock.notifyAll();
}
}
}
}
package com.myTread.abcabc.my;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
Object lock = new Object();
AtomicInteger number = new AtomicInteger(1);
Print a = new Print(lock,1,number,2);
Thread thread1 = new Thread(a,"a");
thread1.start();
Print b = new Print(lock,2,number,3);
Thread thread2 = new Thread(b,"b");
thread2.start();
Print c = new Print(lock,3,number,1);
Thread thread3 = new Thread(c,"c");
thread3.start();
}
}
第二种:用Condition控制
package com.myTread.abcabc.condition;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
public class Print {
private Lock lock;
private Condition conditionA;
private Condition conditionB;
private Condition conditionC;
private int number;
public Print(Lock lock,int number) {
this.lock = lock;
this.conditionA = lock.newCondition();
this.conditionB = lock.newCondition();
this.conditionC = lock.newCondition();
this.number = number;
}
public void printA() throws InterruptedException {
lock.lock();
try {
if (number!= 1) {
conditionA.await();
}
System.out.println(Thread.currentThread().getName());
number = 2;
conditionB.signal();
} finally {
lock.unlock();
}
}
public void printB() throws InterruptedException {
lock.lock();
try {
if (number!= 2) {
conditionB.await();
}
System.out.println(Thread.currentThread().getName());
number=3;
conditionC.signal();
} finally {
lock.unlock();
}
}
public void printc() throws InterruptedException {
lock.lock();
try {
if (number!= 3) {
conditionC.await();
}
System.out.println(Thread.currentThread().getName()+" ");
number=1;
conditionA.signal();
} finally {
lock.unlock();
}
}
}
package com.myTread.abcabc.condition;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class TestCondition {
public static void main(String[] args) throws InterruptedException {
AtomicInteger num = new AtomicInteger(1);
final Print print = new Print(new ReentrantLock(), 1);
Thread a = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 1; i <= 20; i++) {
print.printA();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "a");
Thread b = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 1; i <= 20; i++) {
print.printB();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "b");
Thread c = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
try {
print.printc();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "c");
a.start();
b.start();
c.start();
}
}
三 和第一个差不多
package queue;
import java.util.concurrent.atomic.AtomicInteger;
class PrintRes {
/* public AtomicInteger flag = new AtomicInteger(1);*/
public volatile int flag = 1;
}
class Athread extends Thread {
private PrintRes printRes;
public Athread(PrintRes printRes) {
this.printRes = printRes;
}
@Override
public void run() {
synchronized (printRes) {
for (int i = 1; i <= 10; i++) {
while (printRes.flag != 1) {
try {
printRes.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("a");
printRes.flag = 2;
printRes.notifyAll();
}
}
}
}
class Bthread extends Thread {
private PrintRes printRes;
public Bthread(PrintRes printRes) {
this.printRes = printRes;
}
@Override
public void run() {
synchronized (printRes) {
for (int i = 1; i <= 10; i++) {
while (printRes.flag != 2) {
try {
printRes.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("b");
printRes.flag = 3;
printRes.notifyAll();
}
}
}
}
class Cthread extends Thread {
private PrintRes printRes;
public Cthread(PrintRes printRes) {
this.printRes = printRes;
}
@Override
public void run() {
synchronized (printRes) {
for (int i = 1; i <= 10; i++) {
while (printRes.flag != 3) {
try {
printRes.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("c");
printRes.flag = 1;
printRes.notifyAll();
}
}
}
}
public class TestAbc {
public static void main(String[] args) throws InterruptedException {
PrintRes printRes = new PrintRes();
Athread a = new Athread(printRes);
Bthread b = new Bthread(printRes);
Cthread c = new Cthread(printRes);
a.start();
Thread.sleep(100);
b.start();
Thread.sleep(100);
c.start();
}
}