面试题:子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。
使用synchronized +wait()+notify()
package threadExchangeExecute;
/**
* 子线程循环10次,接着主线程循环100,
* 接着又回到子线程循环10次,
* 接着再回到主线程又循环100,如此循环50次
* synchronized
* @author q
*
*/
public class MainThread {
static int j=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
MainThread mainThread=new MainThread();
Thread coreThread=mainThread.new CoreThread();
coreThread.setName(coreThread.getClass().getName());
Thread subThread=mainThread.new SubThread();
subThread.setName(subThread.getClass().getName());
coreThread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
subThread.start();
}
class CoreThread extends Thread{
public void run() {
synchronized (MainThread.class) {
while (j < 50) {
for (int i = 0; i < 200; i++) {
if (i % 100 == 0) {
//第1次和第101次执行之前先处于等待状态,让子线程先执行
//由子线程执行完第10次和第20次之后notify主线程来执行
MainThread.class.notify();
try {
MainThread.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
show(i);
if (i == 199) {
//第200次执行完后,通知子线程开始下一次循环
System.out.println("大循环execute 第" + (j + 1) + "次");
j++;
}
}
}
}
}
}
class SubThread extends Thread{
public void run() {
synchronized (MainThread.class) {
while (j < 50) {
for (int i = 0; i < 20; i++) {
if (i == 10) {
//执行完第10次之后,通知主线程可以开始排队竞争资源了,自己变为等待状态
MainThread.class.notify();
try {
MainThread.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
show(i);
if (i == 19) {
//执行完第20次之后,通知主线程可以开始排队竞争资源了,自己变为等待状态
MainThread.class.notify();
try {
MainThread.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
public void show(int i)
{
System.out.println(Thread.currentThread().getName() + "execute 第" + (i + 1) + "次");
}
}
使用ReentrantLock和Condition
package threadExchangeExecute;
/**
* 子线程循环10次,接着主线程循环100,
* 接着又回到子线程循环10次,
* 接着再回到主线程又循环100,如此循环50次
* ReentrantLock And Condition
* @author q
*
*/
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import threadExchangeExecute.MainThread.CoreThread;
import threadExchangeExecute.MainThread.SubThread;
public class ByLockCondition {
ReentrantLock lock = new ReentrantLock();
Condition main = lock.newCondition();
Condition sub = lock.newCondition();
int j = 0;
public static void main(String[] arg) {
ByLockCondition mainThread = new ByLockCondition();
Thread coreThread = mainThread.new MainThread();
coreThread.setName(coreThread.getClass().getName());
Thread subThread = mainThread.new SubThread();
subThread.setName(subThread.getClass().getName());
coreThread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
subThread.start();
}
class MainThread extends Thread {
public void run() {
while (j < 50) {
lock.lock();
try {
for (int i = 0; i < 200; i++) {
if (i % 100 == 0) {
sub.signalAll();
main.await();
}
show(i);
if (i == 199) {
System.out.println("大循环execute 第" + (j + 1) + "次");
j++;
}
}
}catch (Exception e) {
} finally {
lock.unlock();
}
}
}
}
class SubThread extends Thread {
public void run() {
while (j < 50) {
lock.lock();
try {
for (int i = 0; i < 20; i++) {
if (i == 10) {
main.signalAll();
sub.await();
}
show(i);
if (i == 19) {
main.signalAll();
sub.await();
}
}
} catch (Exception e) {
} finally {
lock.unlock();
}
}
}
}
public void show(int i) {
System.out.println(Thread.currentThread().getName() + " execute 第" + (i + 1) + "次");
}
}