码迷,mamicode.com
首页 > 编程语言 > 详细

Java中利用synchronized关键字实现多线程同步问题

时间:2014-09-15 21:20:09      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:java   thread   synchronized   

Java中利用synchronized关键字实现多线程同步问题

    Java 中多线程的同步依靠的是对象锁机制,synchronized关键字就是利用了封装对象锁来实现对共享资源的互斥访问。
      下面以一个简单例子来说明多线程同步问题,我们希望在run()方法里加入synchronized关键字来实现互斥访问。
   
package com.clark.thread;

public class MyThread implements Runnable{
    private int threadId;
    
    public MyThread(int id){
        this.threadId = id;
    }
    @Override
    public synchronized void run() {
        //此时关键字synchronized锁住的是this对象,即当前运行线程对象本身
        for (int i = 0; i < 100; i++) {
            if(i % 10 ==0){
                System.out.println();
            }
            System.out.print("Thread ID:"+this.threadId+":"+i+" ");
        }
    }

}
  测试类:
package com.clark.thread;

public class ThreadDemo {
	public static void main(String[] args) throws InterruptedException {
                 //代码中创建了10个线程,而每个线程都持有this对象的对象锁,这不能实现线程的同步。
                 for (int i = 0; i < 10; i++) {
			new Thread(new MyThread(i)).start();
			Thread.sleep(1);
		 }
	}
}
    打印结果部分如下:其线程不是互斥访问的。因此没有达到资源多线程同步效果。。。
Thread ID:1:60 Thread ID:1:61 Thread ID:1:62 Thread ID:1:63 Thread ID:1:64 Thread ID:1:65 Thread ID:1:66 Thread ID:1:67 Thread ID:1:68 Thread ID:1:69 
Thread ID:1:70 Thread ID:1:71 Thread ID:1:72 Thread ID:1:73 Thread ID:1:74 Thread ID:1:75 Thread ID:1:76 
Thread ID:1:77 Thread ID:1:78 Thread ID:1:79 
Thread ID:1:80 Thread ID:1:81 Thread ID:2:0 Thread ID:1:82 Thread ID:1:83 Thread ID:1:84 Thread ID:1:85 Thread ID:1:86 Thread ID:1:87 Thread ID:1:88 Thread ID:1:89 
Thread ID:1:90 Thread ID:1:91 Thread ID:1:92 Thread ID:1:93 Thread ID:1:94 Thread ID:1:95 Thread ID:2:1 Thread ID:2:2 Thread ID:2:3 Thread ID:2:4 Thread ID:2:5 Thread ID:2:6 Thread ID:2:7 Thread ID:2:8 Thread ID:2:9 
从上述得知,要想实现线程同步,必须让这些线程去竞争一个唯一的共享对象锁。
package com.clark.thread;

public class MyThread implements Runnable{
	private int threadId;
	private Object object;//线程之间竞争使用的对象锁
	public MyThread(int id,Object object){
		this.threadId = id;
		this.object = object;
	}
	@Override
	public  void run() {
		/**
		 * 将这个object对象的引用传递给每一个线程对象的lock成员变量
		 * 从而每个线程的lock成员都指向同一个Object对象
		 * 这样就可以让线程去竞争这个唯一的共享的对象锁,从而实现同步。
		 */
		synchronized(object){
			for (int i = 0; i < 100; i++) {
				if(i % 10 ==0){
					System.out.println();
				}
				System.out.print("Thread ID:"+this.threadId+":"+i+" ");
			}
		}
	}

}
package com.clark.thread;

public class ThreadDemo {
	public static void main(String[] args) throws InterruptedException {
		Object obj = new Object();
		for (int i = 0; i < 10; i++) {
			new Thread(new MyThread(i,obj)).start();
			Thread.sleep(1);
		}
	}
}
测试结果如下:
Thread ID:5:60 Thread ID:5:61 Thread ID:5:62 Thread ID:5:63 Thread ID:5:64 Thread ID:5:65 Thread ID:5:66 Thread ID:5:67 Thread ID:5:68 Thread ID:5:69 
Thread ID:5:70 Thread ID:5:71 Thread ID:5:72 Thread ID:5:73 Thread ID:5:74 Thread ID:5:75 Thread ID:5:76 Thread ID:5:77 Thread ID:5:78 Thread ID:5:79 
Thread ID:5:80 Thread ID:5:81 Thread ID:5:82 Thread ID:5:83 Thread ID:5:84 Thread ID:5:85 Thread ID:5:86 Thread ID:5:87 Thread ID:5:88 Thread ID:5:89 
Thread ID:5:90 Thread ID:5:91 Thread ID:5:92 Thread ID:5:93 Thread ID:5:94 Thread ID:5:95 Thread ID:5:96 Thread ID:5:97 Thread ID:5:98 Thread ID:5:99 
Thread ID:6:0 Thread ID:6:1 Thread ID:6:2 Thread ID:6:3 Thread ID:6:4 Thread ID:6:5 Thread ID:6:6 Thread ID:6:7 Thread ID:6:8 Thread ID:6:9 
Thread ID:6:10 Thread ID:6:11 Thread ID:6:12 Thread ID:6:13 Thread ID:6:14 Thread ID:6:15 Thread ID:6:16 Thread ID:6:17 Thread ID:6:18 Thread ID:6:19 
Thread ID:6:20 Thread ID:6:21 Thread ID:6:22 Thread ID:6:23 Thread ID:6:24 Thread ID:6:25 Thread ID:6:26 Thread ID:6:27 Thread ID:6:28 Thread ID:6:29 

从第二段代码可知,同步的关键是多个线程对象竞争同一个共享资源即可,上面的代码中是通过外部创建共享资源,然后传递到线程中来实现。我们也可以利用类成员变量被所有类的实例所共享这一特性,因此可以将
lock用静态成员对象来实现,代码如下所示:修改MyThread.java类如下

package com.clark.thread;

public class MyThread implements Runnable{
	private int threadId;
	private static Object object = new Object();//线程之间竞争使用的对象锁
	public MyThread(int id){
		this.threadId = id;
	}
	@Override
	public  void run() {
		<strong>/**
		 * 将这个object对象的引用传递给每一个线程对象的lock成员变量
		 * 从而每个线程的lock成员都指向同一个Object对象
		 * 这样就可以让线程去竞争这个唯一的共享的对象锁,从而实现同步。
		 */</strong>
		synchronized(object){
			for (int i = 0; i < 100; i++) {
				if(i % 10 ==0){
					System.out.println();
				}
				System.out.print("Thread ID:"+this.threadId+":"+i+" ");
			}
		}
	}

}
结果如下:
Thread ID:0:0 Thread ID:0:1 Thread ID:0:2 Thread ID:0:3 Thread ID:0:4 Thread ID:0:5 Thread ID:0:6 Thread ID:0:7 Thread ID:0:8 Thread ID:0:9 
Thread ID:0:10 Thread ID:0:11 Thread ID:0:12 Thread ID:0:13 Thread ID:0:14 Thread ID:0:15 Thread ID:0:16 Thread ID:0:17 Thread ID:0:18 Thread ID:0:19 
Thread ID:0:20 Thread ID:0:21 Thread ID:0:22 Thread ID:0:23 Thread ID:0:24 Thread ID:0:25 Thread ID:0:26 Thread ID:0:27 Thread ID:0:28 Thread ID:0:29 
Thread ID:0:30 Thread ID:0:31 Thread ID:0:32 Thread ID:0:33 Thread ID:0:34 Thread ID:0:35 Thread ID:0:36 Thread ID:0:37 Thread ID:0:38 Thread ID:0:39 

再来看第一段代码,实例方法中加入sychronized关键字封锁的是this对象本身,而在静态方法中加入sychronized关键字封锁的就是类本身。静态方法是所有类实例对象所共享的,因此线程对象在访问此静态方法时是互斥访问的,从而可以实现线程的同步,代码如下所示:

package com.clark.thread;

public class MyThread implements Runnable{
	private int threadId;
	public MyThread(int id){
		this.threadId = id;
	}
	@Override
	public  void run() {
		start(this.threadId);
	}
	private static synchronized void start(int threadId2) {
		for (int i = 0; i < 100; i++) {
			if(i % 10 == 0){
				System.out.println();
			}
			System.out.print("Thread ID:"+threadId2+":"+i+" ");
		}
	}

}

结果如下=========

Thread ID:0:0 Thread ID:0:1 Thread ID:0:2 Thread ID:0:3 Thread ID:0:4 Thread ID:0:5 Thread ID:0:6 Thread ID:0:7 Thread ID:0:8 Thread ID:0:9 Thread ID:0:10 Thread ID:0:11 Thread ID:0:12 Thread ID:0:13 Thread ID:0:14 Thread ID:0:15 Thread ID:0:16 Thread ID:0:17 Thread ID:0:18 Thread ID:0:19 Thread ID:0:20 Thread ID:0:21 Thread ID:0:22 Thread ID:0:23 Thread ID:0:24 Thread ID:0:25 Thread ID:0:26 Thread ID:0:27 Thread ID:0:28 Thread ID:0:29 Thread ID:0:30 Thread ID:0:31 Thread ID:0:32 Thread ID:0:33 Thread ID:0:34 Thread ID:0:35 Thread ID:0:36 Thread ID:0:37 Thread ID:0:38 Thread ID:0:39 Thread ID:0:40 Thread ID:0:41 Thread ID:0:42 Thread ID:0:43 Thread ID:0:44 Thread ID:0:45 Thread ID:0:46 Thread ID:0:47 Thread ID:0:48 Thread ID:0:49 Thread ID:0:50 Thread ID:0:51 Thread ID:0:52 Thread ID:0:53 Thread ID:0:54 Thread ID:0:55 Thread ID:0:56 Thread ID:0:57 Thread ID:0:58 Thread ID:0:59








Java中利用synchronized关键字实现多线程同步问题

标签:java   thread   synchronized   

原文地址:http://blog.csdn.net/caolipeng_918/article/details/39297401

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!