Java多线程中的读写互斥锁

  • 时间:
  • 浏览:1
  • 来源:uu快3新平台_uu快3诀窍_讨论群

package demo.thread;

public class ThreadA extends Thread {
	private MyService service;
	
	public ThreadA(MyService service) {
		super();
		this.service = service;
	}
	
	@Override
	public void run() {
		service.read();
	}

}

package demo.thread;

public class ThreadB extends Thread {
	private MyService service;
	
	public ThreadB(MyService service) {
		super();
		this.service = service;
	}
	
	@Override
	public void run() {
		service.write();;
	}

}

package demo.thread;

public class Run  {

	public static void main(String[] args)  throws InterruptedException {
		// TODO Auto-generated method stub
		final MyService service = new MyService();
		
		ThreadA a = new ThreadA(service);
		a.setName("A");
		a.start();
		
		Thread.sleep(30);
		
		ThreadB b = new ThreadB(service);
		b.setName("B");
		b.start();
		

	}

}



其它读写,写写,写读都是互斥的哟。

能能不能 读读是并发的,