synchronization

Synchronized的做用範圍. java

  1. 方法

靜態方法,非靜態方法. ide

  1. Instance實例, 做用在類上.class

Synchronized(非靜態instance實例) this

只做用於synchronized代碼塊. atom

Synchronized(靜態實例) spa

Synchronized添加到非靜態方法級別,則全部該類的方法添加了synchronized的非靜態方法都會被鎖定.(注意即便靜態方法添加了synchronized,也不會被鎖定),至關於在該對象上加鎖. code

Synchronized添加到靜態方法級別, 至關於添加到類級別, 因此等同與synchronized(類.class). 對象

這種狀況下若是建立類的多個對象, 調用有鎖的靜態方法則全部的其餘的靜態的synchronized方法或者是synchronized(.class)的代碼塊都會阻塞. rem

package thread import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadTest {

	public int test = 0;

	public static void main(String args[]) throws Exception {
		// MyThread mt = new MyThread();
		// //mt.run(); If we call run like this, the thread is main thread
		// mt.start();
		//
		// MyRunnable mr = new MyRunnable();
		// //mr.run();If we call run like this, the thread is main thread
		// Thread t = new Thread(mr);
		// t.start();

		// MyExecutors.executeThreads();

		Object obj = new Object();
		SynchronizedTest test = new SynchronizedTest(obj);
		SyncTestThread st = new SyncTestThread(test);
		Thread t1 = new Thread(st);
		t1.start();
		SyncTestThread1 st1 = new SyncTestThread1(test);
		Thread t2 = new Thread(st1);
		t2.start();

	}
}

class SyncTestThread implements Runnable {

	SynchronizedTest test;

	public SyncTestThread(SynchronizedTest test) {
		// TODO Auto-generated constructor stub
		this.test = test;
	}

	@Override
	public void run() {

		// synchronized(SynchronizedTest.class) {
		// synchronized(test) {
		try {
			Thread.sleep(100);
			test.test1(1);
		} catch (Exception e) {

		}
		// }
	}
}

class SyncTestThread1 implements Runnable {
	SynchronizedTest test;

	public SyncTestThread1(SynchronizedTest test) {
		this.test = test;
	}

	@Override
	public void run() {
		try {
			Thread.sleep(100);
			test.test(2);
		} catch (Exception e) {

		}

	}
}

class SynchronizedTest {
	private static Object obj = new Object();

	public SynchronizedTest(Object obj) {
		// this.obj = obj;
	}

	public  void test(int name) {
		synchronized(SynchronizedTest.class){
			for (int i = 0; i < 10; i++) {
				try {
					Thread.sleep(1000);
					System.out.println("TEST" + name);
				} catch (Exception e) {

				}
			}
		}
	}

	// public synchronized void test1(int p) {
	public  synchronized  void test1(int p) {
			for (int i = 0; i < 10; i++) {
				try {
					Thread.sleep(1000);
					System.out.println(p);
				} catch (Exception e) {

				}
			}
	}
}

class MyThread extends Thread {
	public void run() {
		System.out.println("This is MyThread ");
		System.out.println("In MyThread Current Thread "
				+ Thread.currentThread().getId());
	}
}

class MyRunnable implements Runnable {

	@Override
	public void run() {
		System.out.println("This is Runnable");
		System.out.println("In Runnable Current Thread "
				+ Thread.currentThread().getId());
	}

}

class MyExecutors {
	public static void executeThreads() throws Exception {
		ExecutorService executorService = Executors.newFixedThreadPool(2);
		FutureTask<String> fts = new FutureTask<String>(new Callable<String>() {

			@Override
			public String call() throws Exception {
				System.out.println("Callable");
				return "Current threadid is " + Thread.currentThread().getId();
			}

		});

		executorService.submit(fts);
		executorService.shutdown();

		ExecutorService es = Executors.newFixedThreadPool(2);
		List<Future<String>> futures = new ArrayList<Future<String>>();
		for (int i = 0; i < 5; i++) {
			futures.add(es.submit(new Task()));
		}
		for (Future<String> future : futures) {
			try {
				System.out.println(future.get());
			} catch (ExecutionException ee) {
				System.err.println(ee.getCause());
			}
		}
		es.shutdown();
	}

	static class Task implements Callable<String> {
		private static AtomicInteger i = new AtomicInteger(1);

		public String call() throws Exception {
			i.incrementAndGet();
			if (i.get() % 2 != 0) {
				throw new RuntimeException("That's odd, I failed.");
			}
			return "I'm done";
		}
	}

}
相關文章
相關標籤/搜索