Skip to content

Synchronized

TuPengXiong edited this page Jun 6, 2017 · 5 revisions
  • 一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,
  • 一个时间内只能有一个线程得到执行。
  • 另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
  • 二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,
  • 另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
  • 三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,
  • 其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
  • 四、第三个例子同样适用其它同步代码块。也就是说,
  • 当一个线程访问object的一个synchronized(this)同步代码块时,
  • 它就获得了这个object的对象锁。
  • 结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
  • 五、 同一个类的不同对象,都可以访问synchronized(this)同步代码块
  • 六、 同步块锁的是同一个对象时 ,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞
/**
 * @ClassName: SynchronizedRunnable
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author tupx
 * @date 2017年6月6日 下午2:08:06
 *
 */
public class SynchronizedRunnable implements Runnable {

	private String code;

	public SynchronizedRunnable() {
	}

	public SynchronizedRunnable(String code) {
		this.code = code;
	}
	@Override
	public void run() {

		if (null != code) {
			sayHello4();
		} else {
			try {
				synchronized (this) {
					System.out.println(Thread.currentThread().getName() + " start");
					Thread.sleep(10000);
					System.out.println(Thread.currentThread().getName() + " end");
				}

			} catch (InterruptedException e) {
				System.out.println("ERROR" + Thread.currentThread().getName());
			}
		}
		
	}

	public void sayHello(String str) {
		System.out.println("hello " + str);
	}
	
	public void sayHello2(String str) {
		synchronized (this) {
			int i = 5;
			while (i-- > 0) {
				System.out.println(Thread.currentThread().getName() + " : " + i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException ie) {
				}
			}
		}
	}
	
	public synchronized void sayHello3(String str) {
		int i = 5;
		while (i-- > 0) {
			System.out.println(Thread.currentThread().getName() + " : " + i);
			try {
				Thread.sleep(500);
			} catch (InterruptedException ie) {
			}
		}
	}
	
	public void sayHello4() {
		synchronized (code) {
			int i = 5;
			while (i-- > 0) {
				System.out.println(Thread.currentThread().getName() + " : " + i);
				try {
					Thread.sleep(500);
				} catch (InterruptedException ie) {
				}
			}
		}
	}

	public static void main(String[] args) {

		/**
		 * 一 结果 
		 * Thread1===>start 
		 * Thread1===>end 
		 * Thread2===>start 
		 * Thread2===>end
		 */
		
		 /* SynchronizedRunnable runnable = new SynchronizedRunnable();
		  Thread thread1 = new Thread(runnable, "Thread1===>");
		  Thread thread2= new Thread(runnable, "Thread2===>"); 
		  thread1.start();
		  thread2.start();
		 */

		/**
		 * 二 结果 
		 * Thread1 start 
		 * hello Thread2 
		 * Thread1 end
		 */

		/*final SynchronizedRunnable thread3 = new SynchronizedRunnable();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				thread3.run();
			}
		}, "Thread1");
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				thread3.sayHello("Thread2");
			}
		}, "Thread2");
		t1.start();
		t2.start();
		*/
		
		/**
		 * 三  结果 
		 *Thread1 start
		 *Thread1 end
		 *Thread2 : 4
		 *Thread2 : 3
		 *Thread2 : 2
		 *Thread2 : 1
		 *Thread2 : 0
		 */

		/*final SynchronizedRunnable thread3 = new SynchronizedRunnable();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				thread3.run();
			}
		}, "Thread1");
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				thread3.sayHello2("Thread2");
			}
		}, "Thread2");
		t1.start();
		t2.start();*/
		
		/**
		 * 四  结果 
		 *Thread1 start
		 *Thread1 end
		 *Thread2 : 4
		 *Thread2 : 3
		 *Thread2 : 2
		 *Thread2 : 1
		 *Thread2 : 0
		 *//*
		final SynchronizedRunnable thread3 = new SynchronizedRunnable();
		Thread t1 = new Thread(new Runnable() {
			public void run() {
				thread3.run();
			}
		}, "Thread1");
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				thread3.sayHello3("Thread2");
			}
		}, "Thread2");
		t1.start();
		t2.start();*/
		
		
		/**
		 * 五  结果 
		 *Thread1 start
		 *Thread2 start
		 *Thread2 end
		 *Thread1 end
		 */
		/*SynchronizedRunnable runnable1 = new SynchronizedRunnable();
		SynchronizedRunnable runnable2 = new SynchronizedRunnable();
		Thread t1 = new Thread(runnable1, "Thread1");
		Thread t2 = new Thread(runnable2, "Thread2");
		t1.start();
		t2.start();*/
		
		
			/**
		 	* 六  结果 
		 	* Thread1 : 4
			* Thread1 : 3
			* Thread1 : 2
			* Thread1 : 1
			* Thread1 : 0
			* Thread2 : 4
			* Thread2 : 3
			* Thread2 : 2
			* Thread2 : 1
			* Thread2 : 0
			*/
		String code = "mycode";
		SynchronizedRunnable runnable1 = new SynchronizedRunnable(code);
		SynchronizedRunnable runnable2 = new SynchronizedRunnable(code);
		Thread t1 = new Thread(runnable1, "Thread1");
		Thread t2 = new Thread(runnable2, "Thread2");
		t1.start();
		t2.start();
		
		
	}
}
Clone this wiki locally