初学多线程,这段代码为什么没有实现方法同步问题?请大侠指教!

luqifeng334 2013-07-12 01:24:47
public class PlusThread extends Thread{
private static int j;
public synchronized void Plus(){
j++;
System.out.println(Thread.currentThread().getName() + "-inc:" + j);
}
public void run(){
int n=0;
while(n<=100){
Plus();
n++;
}
}
public static void main(String[] args){
PlusThread.j = 0;
PlusThread thread1 = new PlusThread();
PlusThread thread2 = new PlusThread();
thread1.start();
thread2.start();
}
}
这段代码经过调试,thread1和thread2之间没有实现j++同步;为什么,错在哪里呢?请大侠赐教!
...全文
166 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luqifeng334 2013-07-12
  • 打赏
  • 举报
回复
多谢,完全释疑了!
  • 打赏
  • 举报
回复
引用 7 楼 luqifeng334 的回复:
可是我没用static修饰方法,也就是plus()并不是类对象的方法?
这个和用static没有关系的,synchronized的用法一个是加在一个方法上面,这时的线程锁就是方法所属的对象,如果方法是static的,那么这个方法就属于对应的类对象,类对象当然只的一个。另一个是写成synchronized代码块,形式就是synchronized(Object){}这时这个里面的代码块的锁就是你自己指定了。就像你用的是PlusThread.class,也就是用的这个类对应的类对象,类对象只有一个呀,所以不管哪个线程来执行这个代码块,用的锁都是这个类对象,同一个锁。这个Plus()方法当然也不是类对象的方法,但是里面的synchronized代码块是用的唯一的类对象作为线程锁啊,所以也是同步的。。。。。
luqifeng334 2013-07-12
  • 打赏
  • 举报
回复
可是我没用static修饰方法,也就是plus()并不是类对象的方法?
  • 打赏
  • 举报
回复
引用 3 楼 luqifeng334 的回复:
多谢灰爷,我是新手,再问一下,为什么把上面代码的方法改为如下又同步了呢,也没有用static修饰啊?多谢! private int j=0; public void Plus(){ synchronized(PlusThread.class){ j++; System.out.println(Thread.currentThread().getName() + "-inc:" + j); //System.out.println(j); } }
我才自学了一年,属于菜鸟级别,最近正好在恶补java的基础呢。。。。。。
  • 打赏
  • 举报
回复
引用 3 楼 luqifeng334 的回复:
多谢灰爷,我是新手,再问一下,为什么把上面代码的方法改为如下又同步了呢,也没有用static修饰啊?多谢! private int j=0; public void Plus(){ synchronized(PlusThread.class){ j++; System.out.println(Thread.currentThread().getName() + "-inc:" + j); //System.out.println(j); } }
PlusThread.class是PlusThread的类对象啊,一个类只有一个类对象,所以也就是说只要执行了synchronized代码块,他们就是用的同一个锁。。。。。。当然是同步的。。。。
luqifeng334 2013-07-12
  • 打赏
  • 举报
回复
多谢灰爷,我是新手,再问一下,为什么把上面代码的方法改为如下又同步了呢,也没有用static修饰啊?多谢! private int j=0; public void Plus(){ synchronized(PlusThread.class){ j++; System.out.println(Thread.currentThread().getName() + "-inc:" + j); //System.out.println(j); } }
toss2000 2013-07-12
  • 打赏
  • 举报
回复
1#正解,晚来一步啊
  • 打赏
  • 举报
回复
首先哥们儿,熬夜不好。。。。。。 对于这个,你用了synchronized来实现同步,而如果你这样用,它的锁就是方法所在对象本身,你new出来了两个PlusThread,这是两个线程,分别调用他们自己内部的Plus()方法,那么两个线程所使用的锁不一样,所以不能实现同步啊,只有两个锁对象是同一个对象那才能达到同步。比如像我下面写的就能同步:
package com.test;

public class PlusThread extends Thread {
	private static int j;

	public static synchronized void Plus() {
		j++;
		System.out.println(Thread.currentThread().getName() + "-inc:" + j);
	}

	public void run() {
		int n = 0;
		while (n <= 100) {
			Plus();
			n++;
		}
	}

	public static void main(String[] args) {
		PlusThread.j = 0;
		PlusThread thread1 = new PlusThread();
		PlusThread thread2 = new PlusThread();
		thread1.start();
		thread2.start();
	}
}
加上static后,这个方法就属于PlusThread类,而不是他的对象,也就是两个线程用的锁是PlusThread对应的类对象,而类对象只有一个啊,所以他们的锁是同一个,则能达到同步的效果。其实你你如果用synchronized 块来写的话,真接在类中定义一个静态成员对象,任意对象都行。。。。说白了想实现同步,就让各线程用的锁是同一个锁。。。。。。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧