[b]第一种情况,synchronized无效[/b]
[code]package com.test;
public class ThreadTest extends Thread {
private int threadNo;
public ThreadTest(int threadNo) {
this.threadNo = threadNo;
}
public static void main(String[] args) throws Exception {
for (int i = 1; i < 10; i++) {
new ThreadTest(i).start();
Thread.sleep(1);
}
}
public synchronized void run() {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}[/code]
[b]第二种情况,synchronized有效[/b]
[code]package com.test;
public class ThreadTest2 extends Thread {
private int threadNo;
private String lock;
public ThreadTest2(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 10; i++) {
new ThreadTest2(i, lock).start();
Thread.sleep(1);
}
}
public void run() {
synchronized (lock) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}
}[/code]
[b]第三种情况,synchronized有效[/b]
[code]package com.test;
public class ThreadTest3 extends Thread {
private int threadNo;
private String lock;
public ThreadTest3(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 20; i++) {
new ThreadTest3(i, lock).start();
Thread.sleep(1);
}
}
public static synchronized void abc(int threadNo) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
public void run() {
abc(threadNo);
}
}[/code]
[b]总结:[/b]
1、对于同步的方法或者代码块来说,必须获得对象锁才能够进入同步方法或者代码块进行操作;
2、如果采用method级别的同步,则对象锁即为method所在的对象(怎么这么别扭),如果是静态方法,对象锁即指method所在的Class对象(唯一);
3、对于代码块则,对象锁即指synchronized(abc)中的abc;
4、因为第一种情况,对象锁即为每一个线程对象,因此有多个,所以同步失效,第二种共用同一个对象锁lock,因此同步生效,第三个因为是static因此对象锁为ThreadTest3的class 对象,因此同步生效。
如上述正确,则同步有两种方式,[b]代码块和方法[/b]
如果是代码块,则对象锁自己指定,一般有些代码为synchronized(this)只有在单态模式才生效;
如果是方法,[b]则分静态和非静态两种[/b]。
静态方法则一定会同步,非静态方法需在单例模式才生效,推荐用静态方法(不用担心是否单例)。
[color=red]不知道以上说的对否,如不对请指点。[/color]
[url]http://www.iteye.com/topic/48750[/url]
[code]package com.test;
public class ThreadTest extends Thread {
private int threadNo;
public ThreadTest(int threadNo) {
this.threadNo = threadNo;
}
public static void main(String[] args) throws Exception {
for (int i = 1; i < 10; i++) {
new ThreadTest(i).start();
Thread.sleep(1);
}
}
public synchronized void run() {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}[/code]
[b]第二种情况,synchronized有效[/b]
[code]package com.test;
public class ThreadTest2 extends Thread {
private int threadNo;
private String lock;
public ThreadTest2(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 10; i++) {
new ThreadTest2(i, lock).start();
Thread.sleep(1);
}
}
public void run() {
synchronized (lock) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
}
}[/code]
[b]第三种情况,synchronized有效[/b]
[code]package com.test;
public class ThreadTest3 extends Thread {
private int threadNo;
private String lock;
public ThreadTest3(int threadNo, String lock) {
this.threadNo = threadNo;
this.lock = lock;
}
public static void main(String[] args) throws Exception {
String lock = new String("lock");
for (int i = 1; i < 20; i++) {
new ThreadTest3(i, lock).start();
Thread.sleep(1);
}
}
public static synchronized void abc(int threadNo) {
for (int i = 1; i < 10000; i++) {
System.out.println("No." + threadNo + ":" + i);
}
}
public void run() {
abc(threadNo);
}
}[/code]
[b]总结:[/b]
1、对于同步的方法或者代码块来说,必须获得对象锁才能够进入同步方法或者代码块进行操作;
2、如果采用method级别的同步,则对象锁即为method所在的对象(怎么这么别扭),如果是静态方法,对象锁即指method所在的Class对象(唯一);
3、对于代码块则,对象锁即指synchronized(abc)中的abc;
4、因为第一种情况,对象锁即为每一个线程对象,因此有多个,所以同步失效,第二种共用同一个对象锁lock,因此同步生效,第三个因为是static因此对象锁为ThreadTest3的class 对象,因此同步生效。
如上述正确,则同步有两种方式,[b]代码块和方法[/b]
如果是代码块,则对象锁自己指定,一般有些代码为synchronized(this)只有在单态模式才生效;
如果是方法,[b]则分静态和非静态两种[/b]。
静态方法则一定会同步,非静态方法需在单例模式才生效,推荐用静态方法(不用担心是否单例)。
[color=red]不知道以上说的对否,如不对请指点。[/color]
[url]http://www.iteye.com/topic/48750[/url]