Last active
December 16, 2015 18:29
-
-
Save manxisuo/5477664 to your computer and use it in GitHub Desktop.
Thread 的方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void main(String[] args) | |
| { | |
| Thread thread = new Thread(new Task()); | |
| thread.start(); | |
| // 中断线程 | |
| thread.interrupt(); | |
| System.out.println("exit main thread."); | |
| } | |
| /* | |
| result: | |
| exit main thread. | |
| do sth. | |
| interrupted! | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void main(String[] args) throws InterruptedException | |
| { | |
| Thread thread = new Thread(new Task()); | |
| thread.start(); | |
| // 等待thread终止 | |
| thread.join(); | |
| System.out.println("exit main thread."); | |
| } | |
| /* | |
| result: | |
| do sth. | |
| exit main thread. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void main(String[] args) throws InterruptedException | |
| { | |
| Thread thread = new Thread(new Task()); | |
| // 启动线程 | |
| thread.start(); | |
| System.out.println("exit main thread."); | |
| } | |
| /* | |
| result: | |
| exit main thread. | |
| do sth. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Task implements Runnable | |
| { | |
| @Override | |
| public void run() | |
| { | |
| try | |
| { | |
| System.out.println("do sth."); | |
| Thread.sleep(3000); | |
| } | |
| catch (InterruptedException e) | |
| { | |
| System.out.println("interrupted!"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment