From be69e5e1ac6ec20313414f0ba0f7518abaa6f68e Mon Sep 17 00:00:00 2001 From: noakcn Date: Mon, 11 Jun 2018 16:10:18 +0800 Subject: [PATCH] add blog --- ...6\254\241start\347\272\277\347\250\213.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "source/_posts/20180611\345\244\232\346\254\241start\347\272\277\347\250\213.md" diff --git "a/source/_posts/20180611\345\244\232\346\254\241start\347\272\277\347\250\213.md" "b/source/_posts/20180611\345\244\232\346\254\241start\347\272\277\347\250\213.md" new file mode 100644 index 0000000..ecb0efa --- /dev/null +++ "b/source/_posts/20180611\345\244\232\346\254\241start\347\272\277\347\250\213.md" @@ -0,0 +1,91 @@ +--- +title: 多次start一个线程会怎么样 +date: 2018-06-11 15:20:22 +tags: +- java +- 多线程 +categories: +- 笔记 +--- + +我们都知道,java中通过多线程来提高cpu的利用率,并且提供了三种实现方式 +- 继承Thread类,重写run方法 +- 实现Runnable接口,重写run方法,利用Thread类来启动 +- 通过Callable和FutureTask创建线程 + +本次例子使用Thread类和Runnable来探讨我们对同一个线程**多次调用start()**会发生什么。 + +# 代码 + +线程例子如下: +``` +public class ThreadDemo implements Runnable { + + @Override + public void run() { + while (true) { + System.out.println(Thread.currentThread().getName() + " 线程启动run"); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} +``` + +测试类 +``` +/** + * 多次调用线程start + */ +public class RepeatedlyRunStart { + + @Test + public void test() { + Thread thread = new Thread(new ThreadDemo()); + thread.start(); + thread.start(); + } +} +``` + +# 结果 +运行结果如下: +``` +Thread-0 线程启动run + +java.lang.IllegalThreadStateException + at java.lang.Thread.start(Unknown Source) + at com.noak.myblogexample.part7.RepeatedlyRunStart.test(RepeatedlyRunStart.java:14) + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) + at java.lang.reflect.Method.invoke(Unknown Source) + at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) + at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) + at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) + at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) + at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) + at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) + at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) + at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) + at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) + at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) + at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) + at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) + at org.junit.runners.ParentRunner.run(ParentRunner.java:363) + at org.junit.runner.JUnitCore.run(JUnitCore.java:137) + at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) + at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) + at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) + at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) +``` + +# 分析 +从运行结果来看,我们第一次的start是成功运行了线程,第二次调用start我们收到了一个异常`IllegalThreadStateException` 测试线程被结束了。 + +`IllegalThreadStateException`异常的意思是线程状态异常。这是因为java中线程是有**生命周期(状态)**的。单我们创建了线程,并执行start后,线程状态就进入了就绪状态,不再是新建状态。 + +不过线程的run方法是可以被反复调用的。 \ No newline at end of file