Skip to content

Instantly share code, notes, and snippets.

@manxisuo
Last active December 16, 2015 18:29
Show Gist options
  • Save manxisuo/5477664 to your computer and use it in GitHub Desktop.
Save manxisuo/5477664 to your computer and use it in GitHub Desktop.
Thread 的方法
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!
*/
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.
*/
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.
*/
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