package cn.edu.hpu.test;
/**
* 要求的操作:
* 子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,
* 接着再回到主线程又循环100次,如此循环50次。
* **/
public class ThreadTest3 {
public static void main(String[] args) {
Output out =new Output();//循环输出类
//注意:这里要保证子线程和主线程类引入的是同一个Output对象
//不然无法共用两把“钥匙”
MainRunnable main=new MainRunnable(out);//主线程
SonRunnable son=new SonRunnable(out);//子线程
new Thread(son).start();//主线程启动
new Thread(main).start();//子线程启动
}
}
class Output{
//两把“钥匙”
static boolean mbegin=false;
static boolean sbegin=true;//第一次子线程开始执行,所以这里默认为true
//主线程循环打印的方法(不可被打断)
public synchronized void doMainWhile(int num) throws InterruptedException {
int i=0;//每次i都要初始化为0,供循环使用
while(mbegin==false){//如果子线程在执行,主线程要等待,直到子线程恢复主线程的钥匙
this.wait();
}
for (i = 1; i <=100; i++) {//开始循环(每次在方法里循环100次)
System.out.println("主线程执行了第"+i+"次循环,总循环为第"+num+"次");
if(i==100){
break;
}
}
if(i==100){//主线程循环打印完毕之后,要让位给子线程执行
sbegin=true;//子线程开始工作
mbegin=false;//主线程停止工作
this.notify();//通知其他线程开始工作
}
}
//子线程循环打印的方法(不可被打断)
public synchronized void doSonWhile(int num) throws InterruptedException {
int j=0;//每次i都要初始化为0,供循环使用
while(sbegin==false){//如果主线程在执行,子线程要等待,直到主线程恢复子线程的钥匙
this.wait();
}
for (j = 1; j <=10; j++) {//开始循环(每次在方法里循环10次)
System.out.println("子线程执行了第"+j+"次循环,总循环为第"+num+"次");
if(j==10){
break;
}
}
if(j==10){//子线程循环打印完毕之后,要让位给主线程执行
sbegin=false;//子线程停止工作
mbegin=true;//主线程开始
this.notify();//通知其他线程开始工作
}
}
}
class MainRunnable implements Runnable{
Output out =null;
MainRunnable(Output out){//将Output对象引入进来
this.out=out;
}
public void run() {
try {
//因为要执行50次要求的操作,每次操作主线程要执行2次,一共50*2=100次
for(int i=1;i<=100;i++){
out.doMainWhile(i%2==0?i/2:(i+1)/2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class SonRunnable implements Runnable{
Output out =null;
SonRunnable(Output out){
this.out=out;
}
public void run() {
try {
//因为要执行50次要求的操作,每次操作子线程要执行2次,一共50*2=100次
for(int i=1;i<=100;i++){
out.doSonWhile(i%2==0?i/2:(i+1)/2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}package cn.edu.hpu.test;
public class ThreadTest4 {
public static void main(String[] args) {
new ThreadTest4().init();
}
public void init()
{
final Business business = new Business();
new Thread(
new Runnable()
{
public void run() {
for(int i=0;i<50;i++)
{
business.SubThread(i);
}
}
}
).start();
for(int i=0;i<50;i++)
{
business.MainThread(i);
}
}
private class Business
{
boolean bShouldSub = true;//这里相当于定义了控制该谁执行的一个信号灯
public synchronized void MainThread(int i)
{
if(bShouldSub)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int j=1;j<=100;j++)
{
System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);
}
bShouldSub = true;
this.notify();
}
public synchronized void SubThread(int i)
{
if(!bShouldSub)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int j=1;j<=10;j++)
{
System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);
}
bShouldSub = false;
this.notify();
}
}
}package cn.edu.hpu.test;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
public class ThreadTest5
{
private static Lock lock = new ReentrantLock();
private static Condition subThreadCondition = lock.newCondition();
private static boolean bBhouldSubThread = false;
public static void main(String [] args)
{
ExecutorService threadPool = Executors.newFixedThreadPool(3);
threadPool.execute(new Runnable(){
public void run()
{
for(int i=0;i<50;i++)
{
lock.lock();
try
{
if(!bBhouldSubThread)
subThreadCondition.await();
for(int j=0;j<10;j++)
{
System.out.println(Thread.currentThread().getName() + ",j=" + j);
}
bBhouldSubThread = false;
subThreadCondition.signal();
}catch(Exception e)
{
}
finally
{
lock.unlock();
}
}
}
});
threadPool.shutdown();
for(int i=0;i<50;i++)
{
lock.lock();
try
{
if(bBhouldSubThread)
subThreadCondition.await();
for(int j=0;j<10;j++)
{
System.out.println(Thread.currentThread().getName() + ",j=" + j);
}
bBhouldSubThread = true;
subThreadCondition.signal();
}catch(Exception e)
{
}
finally
{
lock.unlock();
}
}
}
}来达到我们使用多线程的目的。
转载请注明出处:http://blog.csdn.net/acmman/article/details/52774603
原文:http://blog.csdn.net/acmman/article/details/52774603