标签:简单 ble 多线程 interrupt run catch static try 实现
java线程的创建有两种方式,这里我们通过简单的实例来学习一下。
public class HelloThread extends Thread { @Override public void run() { try { TimeUnit.SECONDS.sleep(1); System.out.println("Hello from a thread!"); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { HelloThread helloThread = new HelloThread(); helloThread.start(); System.out.println("In main thread."); } }
运行的结果如下:
In main thread. Hello from a thread!
public class HelloRunnable implements Runnable { @Override public void run() { try { System.out.println("Hello from a thread!"); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { Thread thread = new Thread(new HelloRunnable()); thread.start(); System.out.println("In main method."); } }
运行的结果如下:
In main method. Hello from a thread!
标签:简单 ble 多线程 interrupt run catch static try 实现
原文地址:http://www.cnblogs.com/huhx/p/baseusejavathread1.html