首页 > 应用开发 > Java > Java 多线程 之 Runnable
转载于 :
package com.thread;/** 注意: 1. 如果要启动一个线程必须调用,start()方法 2. 线程同时运行其实是,CPU分配给每个线程一段时间来顺序执行每个线程 3. 因为java是单继承的,所以为了提高可扩展性,一般使用第二种实现Runnable 的方式 概念上 可以理解为 他们 main MyThread 是同时进行 */public class TestRunnable { public static void main(String[] args) { //实列话一个线程 MyThread2 t = new MyThread2(); Thread thread = new Thread(t, "汽车线程"); thread.start();//启动线程, run() 会自动调用 Thread thread2 = new Thread(t, "火车线程"); thread2.start();//启动线程, run() 会自动调用 try { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + i); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } } }//Alt+Shit+S 可以找到父类的方法class MyThread2 implements Runnable { @Override public void run() { try { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + i); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } } }
转载于 :
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10054744/viewspace-2214447/,如需转载,请注明出处,否则将追究法律责任。