关于synchronized关键字

首先要明确实现多线程是三种方式,一种是继承Thread类,一种是实现Runable(run方法 无返回值,可用get方法获取)接口一种是实现Callable(call方法)接口
1、实现Callable接口的实现方式
public class ThreadsTest implements Callable<String> {

	private String str;
	private int count = 10;
	public ThreadsTest(String str){
		this.str = str;
	}
	@Override
	public String call() throws Exception {
		// TODO Auto-generated method stub
		for(int i=0;i<this.count;i++){
			System.out.println(this.str+"_"+i);
		}
		return str;
	}

	public static void main(String[] args) {
		ExecutorService  exs = Executors.newCachedThreadPool();
		List<Future<String>> al = new LinkedList<Future<String>>();
		for(int i=0;i<10;i++){
			al.add(exs.submit(new ThreadsTest("String "+i)));
		}
		for(Future<String> fu : al){
			try {
				fu.get();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

2、关于synchronized关键字

-当两个并发线程访问同一个对象Object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行,另一个线程必须等待当前线程执行完这个代码块之后才能执行该代码块

-当一个线程访问Object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块

-当一个线程访问Object的一个synchronized(this)同步代码块时,其他线程对object中所有其他synchronized(this)同步代码块的访问将被阻塞

-当一个线程访问Object的一个synchronized(this)同步代码块时,他就获得了这个对象所,其他线程对所有该对象的其他所有被同步的部分都将被阻塞

-以上规则对对象锁同样适用

class ThreadsTest implements Runnable{
	@Override
	public void run() {
		synchronized(this){
			for(int i=0;i<5;i++){
				System.out.println(Thread.currentThread().getName()+"synchronized loop"+i);
			}
		}
		
	}
	public static void main(String[] args) {
		//当两个并发线程访问同一个对象Object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行,另一个线程必须等待当前线程
		//执行完这个代码块之后才能执行该代码块
		ThreadsTest threadsTest = new ThreadsTest();
		Thread thread1 = new Thread(threadsTest);
		Thread thread2 = new Thread(threadsTest);
		thread1.start();
		thread2.start();
	}
}
---------------------

class ThreadsTest{
	public synchronized void test1(){
//		synchronized(this){
			int i=5;
			while((i--) > 0){
				System.out.println(Thread.currentThread().getName()+":"+i);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
//		}
	}
	public synchronized void test2(){
//		synchronized(this){
			int i=5;
			while((i--) > 0){
				System.out.println(Thread.currentThread().getName()+":"+i);
			}
		}
//	}
	public static void main(String[] args) {
		//当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块(test2不加锁)
		//当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其他synchronized(this)同步代码块的访问将被阻塞(test2加锁)
		final ThreadsTest threadsTest = new  ThreadsTest();
		new Thread(new Runnable(){
			@Override
			public void run() {
				threadsTest.test1();
			}
		},"t1").start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				threadsTest.test2();
			}
		},"t2").start();
	}
}
------------------

class ThreadsTest{
	class Inner{
		private synchronized void test1(){
			int i=5;
			while((i--) > 0){
				System.out.println(Thread.currentThread().getName()+":"+i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		private synchronized void test2(){
			int i=5;
			while((i--) > 0){
				System.out.println(Thread.currentThread().getName()+":Inner.test2()="+i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	private void test1(Inner inner){
		synchronized(inner){
			//使用对象锁
			inner.test1();
		}
	}
	private void test2(Inner inner){
		inner.test2();
	}
	public static void main(String[] args) {
		final ThreadsTest threadsTest = new ThreadsTest();
		final Inner inner = threadsTest.new Inner();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				threadsTest.test1(inner);
			}
		},"t1").start();
		new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				threadsTest.test2(inner);
			}
		},"t2").start();
	}
}

文章出处: 点击打开链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值