简体   繁体   English

在 Java 中的线程内调用睡眠时如何启动新线程以继续工作

[英]How to start new Thread to continue work when Sleep is called within a Thread in Java

I am creating a Thread calling a custom compare method within a class - FastestComparator.java我正在创建一个线程,在 class - FastestComparator.java 中调用自定义比较方法

The Thread Class:线程 Class:

public class FastestComparatorThread extends Thread {

    private int valueToFind;
    private List<CustomNumberEntity> list;
    private FastestComparator fastComparator = new FastestComparator();
    private int result = 1;

    public FastestComparatorThread(int valueToFind, List<CustomNumberEntity> list) {
        this.valueToFind = valueToFind;
        this.list = list;
    }

    public int getResult() {
        return result;
    }

    @Override
    public void run() {
        for (CustomNumberEntity customNumberEntity : list) {
            try {
                synchronized(fastComparator) {
                    result = fastComparator.compare(valueToFind, customNumberEntity);
                    System.out.println(result);
                }
            } catch (NumberFormatException e) {}
        }
    }
}

The Compare Method has a Sleep function within it:比较方法里面有一个睡眠 function:

public int compare(int firstValue, CustomNumberEntity secondValue){
        Random random = new Random();
        int mSeconds = (random.nextInt(6)+5)*1000; //milliseconds
        int secondValueAsNumber = Integer.parseInt(secondValue.getNumber());
        try {
            Thread.sleep(mSeconds);
        } catch (InterruptedException e) {
            //error while sleeping. Do nothing.
        }
        return firstValue-secondValueAsNumber;
    }

Currently, after starting the Thread, I am calling the interrupt() method, while the Thread isAlive() essentially to avoid the sleep after starting the Thread目前,启动线程后,我调用的是interrupt()方法,而线程isAlive()本质上是为了避免启动线程后休眠

FastestComparatorThread fastestComparatorThread = new FastestComparatorThread(valueToFind, list);
fastestComparatorThread.start();

while(fastestComparatorThread.isAlive()) {
    fastestComparatorThread.interrupt();
    if (fastestComparatorThread.getResult() == 0) {
        return true;
    }
}

Is there a way of kicking off a new thread that continues the work when the first one sleeps instead of continuously interrupting the one Thread?有没有一种方法可以在第一个线程睡眠时启动一个新线程来继续工作,而不是不断地中断一个线程?

Is there a way of kicking off a new thread that continues the work when the first one sleeps...?有没有办法在第一个睡觉时启动一个新线程来继续工作......?

No. The Thread.sleep(nnnn) call does nothing.不。 Thread.sleep(nnnn)调用什么都不做。 It does nothing for approximately nnnn milliseconds (but never less than nnnn milliseconds), and then it returns.它在大约nnnn毫秒(但绝不会少于nnnn毫秒)内什么都不做,然后返回。 There is no provision in Java for one thread to be automatically notified when some other thread calls sleep() . Java 中没有规定在其他线程调用sleep()时自动通知一个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NetBeans / Java / New提示:在循环中调用Thread.sleep - NetBeans / Java / New hint: Thread.sleep called in loop 从多个线程调用时Thread.sleep()如何工作 - How does Thread.sleep() work when called from multiple threads 如何在具有相同主体的线程内启动新线程? - How can I start a new thread within a thread with the same body? Java线程:在随机时间启动和休眠线程 - Java thread: start and sleep threads at random time 为什么在Java中的线程对象上调用start()时没有立即调用run() - Why is run() not immediately called when start() called on a thread object in java swingWorker中的java - thread.sleep() - java - thread.sleep() within SwingWorker for循环中的Java Thread.sleep() - Java Thread.sleep() within a for loop Java Thread:start() - 如何创建新线程? - Java Thread: start() - How does it create a new thread? 在Java中,如何在sleep()上调用当前线程但仍保持锁定之后其他线程有机会运行? - In Java, how can other thread get chance to run after current thread is called on sleep() but still holds the lock? 使用Thread.sleep(..)时出现“ java.lang.OutOfMemoryError:无法创建新的本机线程” - “java.lang.OutOfMemoryError: unable to create new native thread” when using Thread.sleep(..)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM