有人可以帮助我理解线程如何在java中运行。
我有一个主要课程:
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")
}
和一个线程类:
public class TestThread implements Runnable {
public class TestThread() {
sleep(20000);
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
我怎样才能得到这句话:"嘿线程已经结束"无需等待线程休眠?
答案 0 :(得分:1)
你可以这样做: 调用线程类的start方法,并在Run实现中使线程休眠...
请注意,您的应用将在线程从睡眠状态返回之前终止
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
}
}
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");
}
答案 1 :(得分:0)
这可以说是最简单的形式。
gradeWeight = new Array(5);
gradeWeight[1] = 12;
gradeWeight[2] = 18;
gradeWeight[3] = 20;
gradeWeight[4] = 20;
gradeWeight[5] = 20;