java 打印 abc_启用3个线程循环打印abcabcabc

多线程面试时,经常会考到一个用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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值