线程同步synchronized问题

牛的梦想 2011-07-05 11:06:12
synchronized 的作用不是在线程访问该类的对象是把对象锁定等该语句执行完了以后才解锁的吗 为什么这样写 输出结果还是同时执行的呢 菜鸟学线程 求高手解答

public class TestSynchronized {

public static void main(String[] args) {
T t1 = new T("t1");
T t2 = new T("t2");
t1.start();
t2.start();
}
public void run2() {

}
}
class T extends Thread {

private static int num = 0;

T(String n) {
super(n);
}
public void run() {
synchronized(this) {
num++;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
System.out.println(getName() + "------" + num);
}
}
}
...全文
163 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
langxiashahai 2011-07-05
  • 打赏
  • 举报
回复
this指的不是ts,要用this.getClass()。

改成 synchronized(this.getClass()) 即可。
小笨熊 2011-07-05
  • 打赏
  • 举报
回复
synchronized(this) ,lz这样写不对哦
awusoft 2011-07-05
  • 打赏
  • 举报
回复
不是说了那是类变量吗?
牛的梦想 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yangchao228 的回复:]
汗 你怎么还是锁this 麻烦你锁住变量num可以吗
[/Quote]
我想搞明白 这里this 是不是就是指的ts吗? 我把ts都锁住了 ts里面的num变量不也就锁了吗 呵呵 我是菜鸟学线程~~~~
蒙奇D路飞 2011-07-05
  • 打赏
  • 举报
回复
汗 你怎么还是锁this 麻烦你锁住变量num可以吗
牛的梦想 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 brightyq 的回复:]
引用 1 楼 anhy 的回复:
synchronized(this)锁定的是当前对象


锁一个其它什么对象都行。
[/Quote]
锁住的是当前类的对象 但是我这么写也不行啊 t1 t2 同时调用的是ts类的对象 当t1 开始时候 调用ts类 ts应该被锁定的 怎么还不行呢

public class TestSynchronized {

public static void main(String[] args) {
T t1 = new T("t1");
T t2 = new T("t2");
t1.start();
t2.start();
}

}
class T extends Thread {
TT ts = new TT();
T(String n) {
super(n);
}
public void run() {
ts.run2();
}
}
class TT {
private static int num = 0;
public void run2() {
synchronized(this) {
num++;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
System.out.println("------" + num);
}
}
}
awusoft 2011-07-05
  • 打赏
  • 举报
回复
你可以试试synchronized(this.getClass())
awusoft 2011-07-05
  • 打赏
  • 举报
回复
你锁住自身有什么用啊....你创建了两个对象,你产生同步的问题是num类变量,不是成员变量,锁住this没用
brightyq 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 anhy 的回复:]
synchronized(this)锁定的是当前对象
[/Quote]

锁一个其它什么对象都行。
syoumei 2011-07-05
  • 打赏
  • 举报
回复
给了一下,可能是你要的效果
public class TestSynchronized {

public static void main(String[] args) {
TestSynchronized o = new TestSynchronized();
T t1 = new T("t1",o);
T t2 = new T("t2",o);
t1.start();
t2.start();
}
public void run2() {

}
}
class T extends Thread {

private static int num = 0;
TestSynchronized o;

T(String n,TestSynchronized target) {
super(n);
o=target;
}
public void run() {
synchronized(o) {
num++;
try {
Thread.sleep(1000);
} catch(InterruptedException e) {}
System.out.println(getName() + "------" + num);
}
}
}
anhy 2011-07-05
  • 打赏
  • 举报
回复
synchronized(this)锁定的是当前对象
桃园闲人 2011-07-05
  • 打赏
  • 举报
回复
首先t1和t2是两个对象,你咋他们的类中进行同步,这样他们运行的时候有改同步方法,分别在两个对象中运行,即他们为不同对象的方法,这样你的写法就相当于给他们各自的方法同步,没有意义呀。因为该方法只有他们自己调用,别人没有使用呀,所以吗更没加同步效果一样喽。。。。。
ccnadogteam2 2011-07-05
  • 打赏
  • 举报
回复
两个房间两把钥匙(this)当然两个房间都能打开咯,也就没有锁住,如果你两个房间只有一把钥匙(静态变量)就能锁住了另一个房间
sjkof 2011-07-05
  • 打赏
  • 举报
回复
锁的是本身的对象,程序是用两个对象在调
三心不可得 2011-07-05
  • 打赏
  • 举报
回复
Timer里面还有个add方法?
qybao 2011-07-05
  • 打赏
  • 举报
回复
LZ要明白一个事情,同步是多个线程共同访问一个相同的资源才有意义的
你的第一次写法,syncrhonized(this)就是每个线程自己锁自己(和别的线程没有任何干涉),所以达不到同步的效果

public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run(){
syncronized(this) { //没有这里也不可以
timer.add(Thread.currentThread().getName());
}
}
}

这个例子可以(上面修改后),是因为t1和t2共同使用一个test

楼主好好理解一下,第一次为什么没有共同使用一个对象?为什么上面这段程序是共同使用一个对象?

gengliangyu 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 oiqqwwy 的回复:]

引用 9 楼 awusoft 的回复:
不是说了那是类变量吗?


Java code

public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync(……
[/Quote]

你这样可以,是因为你创建的两个线程访问的是同一个对象的synchronized的方法, 所以可以。

之前不可以,是因为你创建的两个线程访问了两个不同的对象, 每一个线程都访问自己的对象里面的方法,所以锁不住。 如果你想一开始的那个例子也有同步效果的话, 你应该把那个同步块提出来, 把它变成一个静态的同步方法, 就能锁住了。

楼主明白??
90后小朋友 2011-07-05
  • 打赏
  • 举报
回复
学习下 啊哈哈
牛的梦想 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 awusoft 的回复:]
不是说了那是类变量吗?
[/Quote]


public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}

class Timer{
private static int num = 0;
public synchronized void add(String name){
//synchronized (this) {
num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");
//}
}
}
为什么这样用就可以

62,614

社区成员

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

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