主线程 和子线程的同步控制

有道面试题  子线程循环10次 然后 主线程 循环100次  如此往复50次  通过主线程和子线程的同步实现


public class Test 
{
	public static void main(String args[])
	{
		//子线程循环10次,主线程循环100次  如此循环50次
		new Thread( new Runnable(){

			@Override
			public void run() {
				for(int i=0;i<50;i++)
				{
					synchronized(Test.class)
					{
						for(int j=0;j<10;j++)
						{
							System.out.println("sub thread "+i+" loop of "+j);
						}
					}
				}
				
			}
			
		}).start();
		for(int i=0;i<50;i++)
		{
			synchronized(Test.class)
			{
				for(int j=0;j<10;j++)
				{
					System.out.println("main thread "+i+" loop of " +j);
				}
			}
		}
		    
	}
}

通过synchronized字节码的方式 锁定 需要同步的内容显得简单粗暴  这样 在子线程循环的内部的10次的时候不会被打断  而主线程内部循环的10次也不会被子线程打断


第二种方式 把主线程和子线程的方法写到一个类里面  这样就能锁同样的一个对象,synchronized 加载方法前面表示锁这个对象,这种方式更易于细粒度的控制线程

public class Test 
{
	
	public static void main(String args[])
	{
		 final Business business=new Test().new Business();
		//子线程循环10次,主线程循环100次  如此循环50次
		new Thread( new Runnable(){

			@Override
			public void run() {
				for(int i=0;i<50;i++)
				{
					business.sub(i);
				}
				
			}
			
		}).start();
		for(int i=0;i<50;i++)
		{
			business.main(i);
		}
		    
	}
	class Business 
	{
		public synchronized void sub(int i)
		{
			for(int j=0;j<10;j++)
			{
				System.out.println("sub thread "+j+" loop of "+i);
			}
		}
		public synchronized void main(int i)
		{
			for(int j=0;j<10;j++)
			{
				System.out.println("main thread "+j+" loop of " +i);
			}
		}
	}
}


下面的继续  我们上面只是实现了互斥  但是并没有通信, 让子线程和主线程轮流执行 ,接下来 我们通过设置一个bool变量 以及 通过对象this.wait 和this.notifyAll 来控制线程的同步

package uses;

import java.util.Timer;
import java.util.TimerTask;

import org.python.modules.synchronize;
import org.python.util.PythonInterpreter;

public class Test 
{
	
	public static void main(String args[])
	{
		 final Business business=new Test().new Business();
		//子线程循环10次,主线程循环100次  如此循环50次
		new Thread( new Runnable(){

			@Override
			public void run() {
				for(int i=0;i<50;i++)
				{
					business.sub(i);
				}
				
			}
			
		}).start();
		for(int i=0;i<50;i++)
		{
			business.main(i);
		}
		    
	}
	class Business 
	{
		private boolean  bShouldSub=true;
		public synchronized void sub(int i)
		{
			if(bShouldSub)
			{
				//如果这个变量为true  表示该子线程执行
				for(int j=0;j<10;j++)
				{
					System.out.println("sub thread "+j+" loop of "+i);
				}
				//执行完毕 设置bShouldSub为false  
				bShouldSub=false;
				//同时通知 等待this对象的线程
				this.notifyAll();
			}
			else{
				//如果这个变量 为false 表示 主线程正在执行,子线程需要等待
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		public synchronized void main(int i)
		{
			if(bShouldSub)
			{
				//这个时候主线程需要等待
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else
			{
				for(int j=0;j<10;j++)
				{
					System.out.println("main thread "+j+" loop of " +i);
				}
				//执行完毕后 将变量设置为true  让子线程执行
				bShouldSub=true;
				//通知等待的子线程
				this.notifyAll();
			}
			
		}
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值