环境 JVM 1.8
Runable 会占用当前线程,如果run方法循环的话会阻塞当前线程。
Thread 会新启动一个线程。
所以什么线程间数据问题,请自行考虑,那个售票的示例,理解方式感觉就不对
以下最简单的示例
1.Runable
public class MyRunThread implements Runnable{ @Override public void run() { System.out.println("MyRunThread start"); for (int i = 0; i < 10; i++) { System.out.println("MyRunThread :"+i); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("MyRunThread end"); } }
2.Thread
public class MyThrThread extends Thread{ @Override public void run() { System.out.println("MyThrThread 开始"); for (int i = 0; i < 10; i++) { System.out.println("MyThrThread :"+i); try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("MyThrThread 结束"); } }
3.TEST
public class RunTest { public static void main(String[] args) { System.out.println("MyRunThread 启动开始"); MyRunThread run = new MyRunThread(); run.run(); System.out.println("MyRunThread 启动完成"); System.out.println("MyThrThread 启动开始"); MyThrThread thr = new MyThrThread(); thr.start(); System.out.println("MyThrThread 启动完成"); } }
4.结果
MyRunThread start MyRunThread :0 MyRunThread :1 MyRunThread :2
乐享:知识积累,快乐无限。