简体   繁体   English

在线程休眠时执行主类-Java

[英]Executing main class while thread sleeps - java

Can someone help me understand how threads function in java. 有人可以帮助我了解线程如何在Java中起作用。

I have a main class: 我有一个主要的班级:

public static void main(String[] args) {
    Runnable r = new TestThread();
    new Thread(r).start();
    executor.execute(r);
    System.out.println("Hey the thread has ended")
}

and a thread class: 和一个线程类:

public class TestThread implements Runnable {
    public class TestThread() {
        sleep(20000);
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub
    }

}

How can I get the phrase:"Hey the thread has ended" without having to wait while the thread sleeps? 我如何获得这样的短语:“嘿,线程已结束”而不必等待线程休眠?

You can do: call the start method of the thread class, and in the Run implementation make the thread sleep... 您可以执行以下操作:调用线程类的start方法,然后在Run实现中使线程处于睡眠状态...

notice that your app is going to terminate before the Thread returns from the sleep and continues 请注意,在线程从睡眠状态返回并继续之前,您的应用程序将终止

The Runnable class 可运行类

    public class TestThread implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void setParameter(String string) {
        // TODO Auto-generated method stub

    }
}

The implementation 实施

 public static void main(String[] args) {
    TestThread r = new TestThread();
    r.setParameter("myParameterHere");
    Thread t = new Thread(r);
    t.setName("asdas");
    t.start();
    System.out.println("Hey the thread has ended");
}

Here it is in what is arguably it's most simple form. 可以说这是最简单的形式。

public class TestThread implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException ex) {
        }

    }

}

public void test() throws InterruptedException {
    Runnable r = new TestThread();
    Thread t = new Thread(r);
    t.start();
    // No it hasn't!
    //System.out.println("Hey the thread has ended");
    t.join();
    // It has now!
    System.out.println("Hey the thread has ended");
}

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

相关问题 Java Future:如何在执行Aysnc调用时取消阻塞主线程 - Java Future : How to unblock Main thread while executing Aysnc call 使用 java -classpath 命令执行主类时出现 NoClassDefFoundError - NoClassDefFoundError while executing main class using java -classpath command 从Java类执行Oracle过程时线程被阻塞 - BLOCKED thread while executing Oracle procedure from Java class Java线程的睡眠时间比指定时间长 - Java thread sleeps a lot longer than specified Java线程滞后/在Ubuntu / Jetty上长时间睡眠 - Java Thread lags/long sleeps on Ubuntu/Jetty 在DB2中执行查询时,线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException while executing query in DB2 线程“ main”中的异常java.lang.RuntimeException:执行Storm时发现了多个defaults.yaml资源 - Exception in thread “main” java.lang.RuntimeException: Found multiple defaults.yaml resources while executing Storm Java中的主线程在使用Runtime类创建的任何进程完成执行之前是否完成 - Does the main thread in java finish up before any processes it might create using Runtime class finish executing 执行war时无法加载主类 - Failed to load main class while executing war 执行Java类时发生异常 - Exception while executing java class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM