JAVA多线程共用一个线程体的问题

Young_tree 2016-04-28 02:02:24

public class myThread implements Runnable {
public void run() {
int i = 100;
while(true){
System.out.println(Thread.currentThread().getName() + ":" + i);
i--;
Thread.yield();
if (i<0) {
break;
}
}
}
}

public class test {

public static void main(String[] args) {
myThread mt = new myThread();
Thread t1 = new Thread(mt);
Thread t2 = new Thread(mt);
t1.setName("线程1");
t2.setName("线程2");

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



代码示例如上,我理想的运行结果应该是t1 t2交替运行
比如,
线程1:100
线程1:99
线程1:98
线程2:97
线程2:96
线程1:95


但是在eclipse中却是
线程1:100
线程2:100
线程2:99
线程2:98
线程1:99
线程2:97
线程2:96
线程1:98
线程2:95
线程2:94
线程1:97
线程1:96
线程2:93
线程1:95
线程2:92
线程1:94
线程2:91
线程1:93
线程2:90
。。。。。


这是什么原因呢?
线程释放CPU再重新竞争也应该是运行到thread.yield()才会发生吧?
...全文
245 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_34832065 2016-04-29
  • 打赏
  • 举报
回复
在run方法下加。synchronized方法对象锁用this就成。把你执行的代码丢方法body里。
?2020 2016-04-29
  • 打赏
  • 举报
回复
引用 6楼sinat_34653743 的回复:
i设成static试下,好像是这样才是共用一个i
还要加入锁才不会打印相同的数值
?2020 2016-04-29
  • 打赏
  • 举报
回复
i设成static试下,好像是这样才是共用一个i
rickylin86 2016-04-29
  • 打赏
  • 举报
回复

class myThread implements Runnable {
    public void run() {
        while(i > 0){
			synchronized(this){
				currentThreadName = Thread.currentThread().getName();
				if(currentThreadName.equals(threadName)){
					continue;
				}
				System.out.println(currentThreadName + ":" + i --);
				threadName = currentThreadName;
			}
		}
        
    }
	private int i = 100;
	private String threadName = null;
	private String currentThreadName = null;
}
 
public class Test {
 
    public static void main(String[] args) {
        myThread mt = new myThread();
        Thread t1 = new Thread(mt);
        Thread t2 = new Thread(mt);
        t1.setName("线程1");
        t2.setName("线程2");
         
        t1.start();
        t2.start();
    }
}
白虹李李 2016-04-28
  • 打赏
  • 举报
回复
说一个题外话,你这个程序哪怕同步了,也做不到交替的。 多线程就是谁也不知道到底哪个先运行完。也许最后会出现: 1:100 2:99 1:98 2:97 2:96 1:95 这样的。
tangxheng 2016-04-28
  • 打赏
  • 举报
回复
package test; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MyTest { public static void main(String[] args) { MyThread mt = new MyThread(); Thread t1 = new Thread(mt); Thread t2 = new Thread(mt); t1.setName("线程1"); t2.setName("线程2"); t1.start(); t2.start(); } } class MyThread implements Runnable { static int i = 100; Lock lock = new ReentrantLock(); public void run() { while (true) { if (i < 0) { return; } lock.lock();//加锁 System.out.println(Thread.currentThread().getName() + ":" + i); i--; lock.unlock();// 释放锁 Thread.yield(); } } }
Young_tree 2016-04-28
  • 打赏
  • 举报
回复
设置为全局变量之后还是有问题!eclipse中的运行结果变成了 线程2:100 线程1:100 线程2:99 线程1:98 线程2:97 线程1:96 线程2:95 线程1:94 线程1:92 线程2:93 线程1:91 线程2:90 线程1:89 为什么会有两个100呢?
lxraiyl 2016-04-28
  • 打赏
  • 举报
回复
run()方法中你并没有采取任何措施来做同步操作,所以两个线程有可能同时执行。另外java中自减操作是非原子操作
justin_jia_92 2016-04-28
  • 打赏
  • 举报
回复
把int i = 100定义为全局变量

62,628

社区成员

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

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