leetcode中等1115交替打印 FooBar
Posted qq_40707462
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode中等1115交替打印 FooBar相关的知识,希望对你有一定的参考价值。
1115、交替打印 FooBar
交替打印 n 次,一个线程打印 foo ,另一个打印 bar
public class test
public static void main(String[] args)
//a,b这两个runnable
Runnable a = ()->
System.out.print("foo");
;
Runnable b = ()->
System.out.print("bar");
;
FooBar fooBar = new FooBar(11);
//真正start的是这个。lamada表达式创建的匿名类
new Thread(()->
try
fooBar.bar(b);
catch (InterruptedException e)
e.printStackTrace();
).start();
new Thread(()->
try
fooBar.foo(a);
catch (InterruptedException e)
e.printStackTrace();
).start();
class FooBar
private int n;
private Object obj = new Object();
private volatile boolean fooExec = true;
public FooBar(int n)
this.n = n;
public void foo(Runnable printFoo) throws InterruptedException
for (int i = 0; i < n; i++)
synchronized (obj)
if (!fooExec) //fooExec为false时,该线程等待,为true的时候执行下面的操作
obj.wait();
printFoo.run();
fooExec = false;
obj.notifyAll();//唤醒其他线程
public void bar(Runnable printBar) throws InterruptedException
for (int i = 0; i < n; i++)
synchronized (obj)
if (fooExec)
obj.wait();
printBar.run();
fooExec = true;
obj.notifyAll();
补充:两个线程循环交替打印1-100。
public class DoubleThreadPrintNumber
public static int i = 1;
public static Object lock = new Object();
private static AtomicInteger num = new AtomicInteger(1);
private static CountDownLatch countDownLatch = new CountDownLatch(2);
public static void main(String[] args) throws InterruptedException
System.out.println("请输入要执行的操作:");
System.out.println("1、使用CountDownLatch实现");
System.out.println("2、使用syncchronized实现");
System.out.println("3、使用AtomicInteger实现");
//创建Scanner对象,接受从控制台输入
Scanner input=new Scanner(System.in);
//接受String类型
String str=input.next();
switch (str)
case "1": doCountDownLatch();
break;
case "2":doSynchronized() ;break;
case "3":doAtomicInteger();
break;
default:doAtomicInteger();
/**
* countdownlatch 实现
*/
private static void doCountDownLatch()
Thread t1 = new Thread()
@Override public void run()
while (num.intValue() < 100)
if (num.intValue() % 2 == 1)
System.out.println("奇数线程:" + num.intValue());
num.incrementAndGet();
countDownLatch.countDown();
;
Thread t2 = new Thread()
@Override public void run()
while (num.intValue() <= 100)
if (num.intValue() % 2 == 0)
System.out.println("偶数线程:" + num.intValue());
num.incrementAndGet();
countDownLatch.countDown();
;
t1.start();
t2.start();
try
countDownLatch.await();
catch (Throwable e)
System.out.println(e);
/**
* synchronized 关键字实现
*/
private static void doSynchronized()
int TOTAL = 100;
Thread thread1 = new Thread(() ->
while (i <= TOTAL)
synchronized (lock)
if (i % 2 == 1)
System.out.println("i=" + i++);
lock.notify();
System.out.println("奇数打印完毕,释放锁");
else
try
System.out.println("奇数锁等待");
lock.wait();
catch (InterruptedException e)
e.printStackTrace();
);
Thread thread2 = new Thread(() ->
while (i <= TOTAL)
synchronized (lock)
if (i % 2 == 0)
System.out.println("i=" + i++);
lock.notify();
System.out.println("偶数打印完毕,释放锁");
else
try
System.out.println("偶数锁等待");
lock.wait();
catch (InterruptedException e)
e.printStackTrace();
);
thread1.start();
thread2.start();
/**
* AtomicInteger实现
*/
public static void doAtomicInteger()
ReentrantLock reentrantLock = new ReentrantLock();
Condition condition = reentrantLock.newCondition();
new Thread(new Runnable()
int i =1;
// 第一个线程
@Override public void run()
for ( ;num.intValue()<=100;)
if (num.intValue()%3==0)
// reentrantLock.lock();
System.out.println(num.intValue());
num.addAndGet(1);
//reentrantLock.unlock();
else
).start();
// 第二个线程
new Thread(new Runnable()
@Override public void run()
for (;num.intValue()<=100;)
if (num.intValue()%3==1)
System.out.println(num.intValue());
num.addAndGet(1);
// reentrantLock.unlock();
else
// reentrantLock.lock();
).start();
// 第二个线程
new Thread(new Runnable()
@Override public void run()
for (;num.intValue()<=100;)
if (num.intValue()%3==2)
System.out.println(num.intValue());
num.addAndGet(1);
// reentrantLock.unlock();
else
// reentrantLock.lock();
).start();
以上是关于leetcode中等1115交替打印 FooBar的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(多线程)- 1115. 交替打印 FooBar