Synchronized的作用范围.
- 方法
静态方法,非静态方法.
- Instance实例, 作用在类上.class
Synchronized(非静态instance实例)
只作用于synchronized代码块.
Synchronized(静态实例)
Synchronized添加到非静态方法级别,则所有该类的方法添加了synchronized的非静态方法都会被锁定.(注意即使静态方法添加了synchronized,也不会被锁定),相当于在该对象上加锁.
Synchronized添加到静态方法级别, 相当于添加到类级别, 所以等同与synchronized(类.class).
这种情况下如果创建类的多个对象, 调用有锁的静态方法则所有的其他的静态的synchronized方法或者是synchronized(类.class)的代码块都会阻塞.
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";
}
}
}