Java多线程面试题:子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, 接着再回到主线程又循环100,如此循环50次

近在学习多线程,特贴在这里,并附上自己所作答案(参考与张孝祥老师的方法)。


题目如下:

子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, 接着再回到主线程又循环100,如此循环50次


思路如下:

子线程语主线程为互斥,可用SYNCHRONIZED。很容易想到如下代码

package concurrent;

public class theFirstIdea{

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		new Thread(//子线程
				new Runnable(){
					public void run(){
						for(int i=1;i<=50;i++){
							synchronized(theFirstIdea.class){
								for(int j=1;j<=10;j++){
									System.out.println("sub thread: "+i+",loop: "+j);
								}
							}
						}
					}
				}
				).start();

		new Thread(//主线程
				new Runnable(){
					public void run(){
						for(int i=1;i<=50;i++){
							synchronized(theFirstIdea.class){
								for(int j=1;j<=100;j++){
									System.out.println("main thread: "+i+",loop: "+j);
								}
							}
						}
					}
				}
				).start();
	}

}


  由于运行结果很长(有5500行),所以在Eclipse 编译器无法全部看到,或看到的并不是最终运行结果。所以可以在Run -- Run configuration -- Common --勾选File,点击File System.选择到你想保存运行结果的地方,比如桌面,命名为1.txt. 此时桌面将会生产一个名为1.txt的文件,再次运行程序后,运行结果将保存到此文件中。便于查看。

  查看后发现,基本达到要求,但并没有交替执行子线程和主线程。

  而且上述代码不好,没有体现java 的高类聚性,最好能将共同数据或共同方法归为同一类,即编写一个类来存放两个线程,便于修改。代码如下

package concurrent;

public class theFirstIdea{

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		final MyThread threads=new MyThread();
		new Thread(//子线程
				new Runnable(){
					public void run(){
						for(int i=1;i<=50;i++){
							threads.subThread(i);
						}
					}
				}	
				).start();

		for(int i=1;i<=50;i++){
			threads.mainThread(i);
		}
	}
}

class MyThread{
	public synchronized void subThread(int i){
		for(int j=1;j<=10;j++){
			System.out.println("sub thread: "+i+",loop: "+j);
		}
	}
	public synchronized void mainThread(int i){
		for(int j=1;j<=10;j++){
			System.out.println("main thread: "+i+",loop: "+j);
		}
	}
}



要让他们交替进行,可用信号量控制,并用wait  ,notify 进行线程间通信。易得

//子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,
//接着再回到主线程又循环100,如此循环50次,请写出程序。

public class ThreadTest{


	public static void main(String[] args) {

		final MyThread threads=new MyThread();
		new Thread(
				new Runnable(){
					public void run(){
						for(int i=1;i<=50;i++){
							threads.subThread(i);
						}
					}
				}
				).start();
		new Thread(new Runnable(){
			public void run(){
				for(int i=1;i<=50;i++){
					threads.mainThread(i);
				}
			}
		}).start();
	}
}

class MyThread{
	boolean bShouldSub=true;//标志子线程方法是否被调用
	public synchronized void subThread(int i){
		if(!bShouldSub){//若子线程没被调用,即主线程正在运行,所以等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(int j=1;j<=10;j++){
			System.out.println("sub thread :"+i+",loop : "+j);
		}
		bShouldSub=false;//子线程运行完毕
		this.notify();//唤醒其他线程,即主线程
	}
	public synchronized void mainThread(int i){
		if(bShouldSub){//若子线程正在被调用,所以等待
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(int j=1;j<=100;j++){
			System.out.println("main thread :"+i+",loop : "+j);
		}
		bShouldSub=true;//主线程调用完毕
		this.notify();//唤醒子线程
	}
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值