java 饥饿现象,Java线程产生/饥饿问题

我正在编写一个运行多线程银行的代码。我首先用一个程序创建一个线程数组,然后将它们传递给另一个运行循环来启动它们的线程。对于应用程序的一部分,我有一个CPU密集型方法,基本上在一个循环内运行一系列循环。唯一的问题是,出于某种原因,它不会产生我认为应该的方式。以下是运行线程的代码:

public void run(){

this.setPriority(MAX_PRIORITY);

int count = 0;

while(count

int copy = count;

if(transactions[copy] instanceof Jumbler){

System.out.println(copy + " is a jumbler.");

}

else{

System.out.println(copy + " is not a jumbler");

}

transactions[copy].run();

count++;

}

}那么这里是Jumbler运行方法:

public void run(){

System.out.println("running jumbler");

Thread.yield();

Thread.currentThread().yield();

try{

Thread.currentThread().sleep(5000);

}catch(InterruptedException e){}

//this.setPriority(MIN_PRIORITY);

System.out.println("still running.");

Thread.yield();

nums = new int[1000];

int i = 0;

do{

Thread.yield();

for(int x=0;x<1000;x++){

Thread.yield();

//System.out.println("in the loop");

nums[x]=(int)(Math.random()*10000)+1;

for(int y = 0;y<1000;y++){

Thread.yield();

//System.out.println("in the the loop");

for(int z = 0;z<100;z++){

Thread.yield();

}

}

}

Thread.yield();

i++;

System.out.println(whichJumble + ": " + i);

}while(i<1000);

}所以,问题是我希望它产生,让主方法继续运行更多的线程,但它会阻止并等待Jumbler完成(这需要很长时间)。任何想法为什么会发生或如何解决它?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值