Java 多线程问题

crazyboy2005 2011-02-09 04:41:46
程序:
package debug;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T1 t11 = new T1();
T2 t22 = new T2();
Thread t1 = new Thread(t11);
Thread t2 = new Thread(t22);
t1.setName("t1");
t2.setName("t2");

t1.start();
t2.start();
}

}
class T1 implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
Timer1 time = new Timer1();
time.add(Thread.currentThread().getName());

try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName());
}

time.minus(Thread.currentThread().getName());

}
}
class T2 implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
Timer1 time = new Timer1();
time.minus(Thread.currentThread().getName());

try{
Thread.sleep(1000);
}catch(InterruptedException e){System.out.println(Thread.currentThread().getName());}

time.add(Thread.currentThread().getName());

}

}
class Timer1{
public static int num = 0;
public void add(String name){
System.out.println("before add() name: " + name +" num: " + Timer1.num);
Timer1.num += 1;
System.out.println("after add() name: " + name +" num: " + Timer1.num);
System.out.println("name: " + name +" num: " + Timer1.num);
}
public void minus(String name){
System.out.println("before minus() name: " + name +" num: " + Timer1.num);
Timer1.num -= 1;
System.out.println("after minus() name: " + name +" num: " + Timer1.num);
System.out.println("name: " + name +" num: " + Timer1.num);
}
}


运行结果一:
before minus() name: t2 num: 0
after minus() name: t2 num: -1 //这里进行了减法
name: t2 num: -1
before add() name: t1 num: 0 //怎么变成0了呢
after add() name: t1 num: 0 //加了之后,怎么还是0
name: t1 num: 0
before add() name: t2 num: 0
after add() name: t2 num: 1
name: t2 num: 1
before minus() name: t1 num: 0 // 不懂怎么变成0了呢
after minus() name: t1 num: 0 // 减了之后,怎么还是0
name: t1 num: 0

本人刚接触JAVA的线程,运行的结果,跟自己想的有些出入,请各位指点一二,在此谢谢了!
...全文
165 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kai27ks 2011-02-10
  • 打赏
  • 举报
回复
因为你是2个线程在随机争抢资源。你那个sleep只是每个线程获得资源后才有可能执行的(也可能还未执行到sleep就被其他线程占用CPU资源) 所以你那些输出的东西不稳定是正常的。
qingyuan18 2011-02-10
  • 打赏
  • 举报
回复
楼上几位说得很详细了,解决方法两种:Timer1里面的加减方法加sychornized,或者你的Timer1的num变量加sychornized

sychornized的意义就是加锁,一个线程用完释放锁后其他线程才能用,你的实例里面方法上加锁比变量上加锁要好
i李小武 2011-02-10
  • 打赏
  • 举报
回复
兄弟,你的Timer1里面的方法确实没有同步,试试下面的代码

class Timer1{
public static int num = 0;
public synchronized void add(String name){
System.out.println("before add() name: " + name +" num: " + Timer1.num);
Timer1.num += 1;
System.out.println("after add() name: " + name +" num: " + Timer1.num);
System.out.println("name: " + name +" num: " + Timer1.num);
}
public synchronized void minus(String name){
System.out.println("before minus() name: " + name +" num: " + Timer1.num);
Timer1.num -= 1;
System.out.println("after minus() name: " + name +" num: " + Timer1.num);
System.out.println("name: " + name +" num: " + Timer1.num);
}
}
gamefx 2011-02-10
  • 打赏
  • 举报
回复
你在线程中在不停的创建新的线程~~~
guoyifeng 2011-02-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 crazyboy2005 的回复:]
程序:

Java code
package debug;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T1 t1……
[/Quote]

处理这个要用 synchronized ,否则t1,t2 会疯抢CPU的运行时间片儿,致使num的值不一样,这时就要用线程同步了.
cgh804 2011-02-10
  • 打赏
  • 举报
回复
Java的线程同步(synchronizable)问题,建议去查下相关的资料.
crazyboy2005 2011-02-09
  • 打赏
  • 举报
回复
这个程序有3个线程: main, t1, t2, 其中t1,t2影响num的值
首先t2取得执行权:
before minus() name: t2 num: 0
after minus() name: t2 num: -1 //这里进行了减法
name: t2 num: -1
这里t2线程sleep(), 数据区里的num值为-1
关键是下面这个输出,前面t2线程在sleep(),对num的值就没有影响了,下面执行t1线程的时候,怎么数据区里的num就变成0了呢?
before add() name: t1 num: 0 //怎么变成0了呢
这中间执行了加法,怎么数据区的num值还是0?
after add() name: t1 num: 0 //加了之后,怎么还是0
uastation 2011-02-09
  • 打赏
  • 举报
回复
出现这种原因,首先是因为你的线程中的资源出现互相争用,线程之间没有同步,使用同步的方法之一:使用synchronized关键字,在方法名前面;如3楼正解;
老紫竹 2011-02-09
  • 打赏
  • 举报
回复
多线程,最好不要使用公用的东西(大家各干各的),否则肯定有争抢和竞争。

如果必须公用,那只能同步(大家排队啦,我管你几个人来)
duanjingyu 2011-02-09
  • 打赏
  • 举报
回复
一楼正解
冰思雨 2011-02-09
  • 打赏
  • 举报
回复
线程同步问题
Inhibitory 2011-02-09
  • 打赏
  • 举报
回复
package debug;

public class Test {
public static void main(String[] args) {
T1 t11 = new T1();
T2 t22 = new T2();
Thread t1 = new Thread(t11);
Thread t2 = new Thread(t22);
t1.setName("t1");
t2.setName("t2");

t1.start();
t2.start();
}

}

class T1 implements Runnable {

@Override
public void run() {
Timer1 time = new Timer1();
time.add(Thread.currentThread().getName());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName());
}

time.minus(Thread.currentThread().getName());

}
}

class T2 implements Runnable {

@Override
public void run() {
Timer1 time = new Timer1();
time.minus(Thread.currentThread().getName());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName());
}

time.add(Thread.currentThread().getName());

}

}

class Timer1 {
public static int num = 0;

// 使用synchronized关键字,表示add和minus不能被同时调用,只能按顺序调用
synchronized public void add(String name) {
System.out.println("before add() name: " + name + " num: " + Timer1.num);
Timer1.num += 1;
System.out.println("after add() name: " + name + " num: " + Timer1.num);
System.out.println("name: " + name + " num: " + Timer1.num);
}

synchronized public void minus(String name) {
System.out.println("before minus() name: " + name + " num: " + Timer1.num);
Timer1.num -= 1;
System.out.println("after minus() name: " + name + " num: " + Timer1.num);
System.out.println("name: " + name + " num: " + Timer1.num);
}
}
qq82557566 2011-02-09
  • 打赏
  • 举报
回复
楼上正解。
zqfddqr 2011-02-09
  • 打赏
  • 举报
回复
这位兄弟你的多线程没有同步
情况是随机的

before minus() name: t2 num: 0
after minus() name: t2 num: -1

两个输出之间有其他的线程运行

num值是不停的变化滴没有稳定结果

比如我运行的结果是

before add() name: t1 num: 0
after add() name: t1 num: 1
name: t1 num: 1
before minus() name: t2 num: 0
after minus() name: t2 num: 0
name: t2 num: 0
before add() name: t2 num: 0
after add() name: t2 num: 1
name: t2 num: 1
before minus() name: t1 num: 0
after minus() name: t1 num: 0
name: t1 num: 0

要想有稳定的结果你的做同步呵呵

62,614

社区成员

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

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