下面这三个类都是从网上看的帖子中的,我的理解是第二个类是在test对象上创建了十个线程,每个线程要执行同步方法,都要等待test对象的锁;第三个类是创建了十个不同的test1对象,并且在test1对象上都创建了一个线程,所以十个线程都可以在各自的test1对象上同步执行,而不用去等待其他test1对象上的锁。要是按这样理解的话,那么在第一个类中锁定的对象就应该是inc和dec了,但是我有看到同步方法在ThreadTest中,觉得要锁定的对象就应该是tt了,到底是锁定那个对象,脑子中没有一个清晰的判断标准,各位高手都是怎么理解下面这三个类中是锁定那个对象的,最好能详细的解释一下,或是给一个写的比较好的帖子,上面第一段话是我自己的理解,不知道对不对,不对的话希望能指出那个地方错了,感谢您的回复,谢谢!!!
1:
public class ThreadTest {
private int j; public static void main(String args[]) {
ThreadTest tt = new ThreadTest();
Inc inc = tt.new Inc();
Dec dec = tt.new Dec();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.start();
t = new Thread(dec);
t.start();
}
} private synchronized void inc() {
j++;
System.out.println(Thread.currentThread().getName() + "-inc:" + j);
} private synchronized void dec() {
j--;
System.out.println(Thread.currentThread().getName() + "-dec:" + j);
} class Inc implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
inc();
}
}
} class Dec implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
dec();
}
}
}
} 2:
public class Test1 implements Runnable {
public void run() {
synchronized(this) {
try {
System.out.println(System.currentTimeMillis());
Thread.sleep(2000);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Test1 test=new Test1();
for(int i=0;i<10;i++) {
new Thread(test).start();
}
}
}3:
public class Test1 implements Runnable {
public void run() {
synchronized(this) {
try {
System.out.println(System.currentTimeMillis());
Thread.sleep(2000);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
for(int i=0;i<10;i++) {
new Thread(new Test1()).start();
}
}
}
1:
public class ThreadTest {
private int j; public static void main(String args[]) {
ThreadTest tt = new ThreadTest();
Inc inc = tt.new Inc();
Dec dec = tt.new Dec();
for (int i = 0; i < 2; i++) {
Thread t = new Thread(inc);
t.start();
t = new Thread(dec);
t.start();
}
} private synchronized void inc() {
j++;
System.out.println(Thread.currentThread().getName() + "-inc:" + j);
} private synchronized void dec() {
j--;
System.out.println(Thread.currentThread().getName() + "-dec:" + j);
} class Inc implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
inc();
}
}
} class Dec implements Runnable {
public void run() {
for (int i = 0; i < 100; i++) {
dec();
}
}
}
} 2:
public class Test1 implements Runnable {
public void run() {
synchronized(this) {
try {
System.out.println(System.currentTimeMillis());
Thread.sleep(2000);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
Test1 test=new Test1();
for(int i=0;i<10;i++) {
new Thread(test).start();
}
}
}3:
public class Test1 implements Runnable {
public void run() {
synchronized(this) {
try {
System.out.println(System.currentTimeMillis());
Thread.sleep(2000);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
for(int i=0;i<10;i++) {
new Thread(new Test1()).start();
}
}
}
同步并不是锁定哪个对象。而是使用哪个对象作为监视器进行锁定,每个对象都有一把隐式的锁,每一把锁只能让一个线程获得,当这把锁被别的线程拿走时,其他的线程就会在这里进行等待,直到那个对象释放掉这把锁。
http://topic.csdn.net/u/20100424/11/52aeb834-ff0b-40ad-a31f-396907e61ed8.html关于java多线程的一个生产者和多个消费者
http://topic.csdn.net/u/20100426/16/6868d364-ee84-409b-adb3-dcef3dea4026.html
2、类级别的锁就是所有该类对象的全部锁
2、synchronized 放在方法上,拿到的就是调用这个方法的对象的锁。
3、一个方法想获得某对象的锁,而这个锁却被其他方法占用,这个方法就会进入锁池等待,而产生同步
非静态方法锁定当前对象
2理解的不正确,《深入java虚拟机 第二版》有介绍