Java 线程屈服/饥饿问题

发布于 2024-11-04 15:32:35 字数 1670 浏览 0 评论 0原文

我正在编写一个将运行多线程银行的代码。我首先使用一个程序创建一组线程,然后将它们传递到另一个运行循环来启动它们的线程中。对于应用程序的一部分,我有一个 CPU 密集型方法,基本上在彼此之间运行一系列循环。唯一的问题是,由于某种原因,它没有按照我认为应该的方式产生。这是运行线程的代码:

public void run(){
    this.setPriority(MAX_PRIORITY);
    int count = 0;

    while(count<transactions.length){
        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 run 方法:

    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);
}

所以,问题是我希望它屈服,允许 main 方法继续运行更多线程,但它会阻塞并等待 Jumbler 完成(这需要很长时间)。知道为什么会发生这种情况或如何解决它吗?

I'm writing a code that will run a multithreaded bank. I first create an array of threads with one program, then pass them into another thread that runs a loop to start them. For part of the application, I have a CPU intensive method that basically runs a series of loops within one another. Only problem is, for some reason it is not yielding the way that I think it should. Here is the code that is running the threads:

public void run(){
    this.setPriority(MAX_PRIORITY);
    int count = 0;

    while(count<transactions.length){
        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++;
    }

    }

Then here is the Jumbler run method:

    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);
}

So, the problem is that I want it to yield, allowing the main method to continue running more threads, but it blocks and waits for the Jumbler to complete (which takes a long time). Any idea why that would happen or how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃气十足 2024-11-11 15:32:35

我认为问题出在主循环中的 transactions[copy].run(); 中。这个直接调用 run 方法,而不是在另一个系统线程中。而是使用 transactions[copy].start(); 启动线程。

I suppose the issue comes with transactions[copy].run(); in your main loop. This one calls the run method directly but not in another system thread. Instead start the thread with transactions[copy].start();.

你げ笑在眉眼 2024-11-11 15:32:35

看起来您正确地生成了线程(事实上,您根本没有生成它们)

如果您希望线程开始运行(与当前线程并发),您需要调用 start()< /code> 该 Thread 对象的方法,而您没有。

如果我正确理解您的代码,您希望第一个片段生成其他线程。因此,您应该将 transactions[copy].run() 更改为 transactions[copy].start()

(这是一个有根据的猜测。如果您显示了 transaction 数组的定义,那就太好了。)

这是启动多个线程的典型方案:

class MyThread extends Thread {
  public void run() { 
    // Do something here ...
  }
}


// Prepare the array
MyThread[] arr = new MyThread[10];
for(int i = 0; i < arr.length; ++i)
  arr[i] = new MyThread();

...

// Launch the threads
for(int i = 0; i < arr.length; ++i)
  arr[i].start();

It seems that you're spawning the thread correctly (in fact, you're not spawning them at all)

If you want a Thread to start running (concurrently to the current thread) you need to call the start() method of that Thread object, which you don't.

If I understand your code correctly, you want the first snippet to spawn the other threads. Therefore you should change transactions[copy].run() to transactions[copy].start().

(This an educated guess. It would be nice if you showed the definition of the transaction array.)

Here's the typical scheme of launching several Threads:

class MyThread extends Thread {
  public void run() { 
    // Do something here ...
  }
}


// Prepare the array
MyThread[] arr = new MyThread[10];
for(int i = 0; i < arr.length; ++i)
  arr[i] = new MyThread();

...

// Launch the threads
for(int i = 0; i < arr.length; ++i)
  arr[i].start();
金兰素衣 2024-11-11 15:32:35

一旦线程运行,我认为不能保证调用 setPriority 时优先级会发生变化。

这两个语句做同样的事情:

Thread.yield();
Thread.currentThread().yield();

但是你可能不应该调用yield,让操作系统来做。

Once the thread is running, i don't think you can be guaranteed that priority changes when you call setPriority.

these two statements do the same thing:

Thread.yield();
Thread.currentThread().yield();

but you probably shouldn't be calling yield, let the os do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文