代码示例:
class MyThread extends Thread{ private String name; public MyThread(String name){ this.name = name; } @Override public void run(){ for(int i=0;i<7;i++){ System.out.println("线程开始:"+this.name+"次数"+i); } } }
class MyRun implements Runnable{ @Override public void run(){ for(int i=0;i<7;i++){ System.out.println("线程开始:"+Thread.currentThread().getName()+"次数"+i); } } }
public class A{ public static void main(String [] args){ MyThread myThread = new MyThread("myThread"); myThread.start(); MyRun myRun = new MyRun(); Thread run1 = new Thread(myRun, "myRun"); run1.start(); } }运行结果:
public class A{ public static void main(String [] args){ MyThread myThread1 = new MyThread(); myThread1.run(); MyThread myThread2 = new MyThread(); myThread2.run(); } }
class MyThread extends Thread{ public void run(){ for(int i=0;i<7;i++){ System.out.println("线程:"+Thread.currentThread().getName()+"次数"+i); } } }
start() : 通过调用start0()启动一个新线程,新线程会执行相应的run()方法。start()不能被重复调用。
run(): run()和普通的成员方法一样可重复调用。单独调用run()会在当前线程中执行run(),并不会启动新线程!
相关源码:
public synchronized void start() { if (threadStatus != 0) //start()方法不能被重复调用,否则会抛出异常 throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); //加入到线程组中 boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } } private native void start0(); @Override public void run() { if (target != null) { //target是当前的Runnable对象 target.run(); } }
卖票问题:
1:Thread
class MyThread extends Thread{ private int tickets =5; @Override public void run() { while (tickets>0) { tickets--; System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets); } } }
public static void main(String [] args){ MyThread myThread1 = new MyThread(); myThread1.start(); MyThread myThread2 = new MyThread(); myThread2.start(); }
class MyThread extends Thread{ private int tickets =5; @Override public void run() { while (tickets>0) { tickets--; System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets); } } }
public static void main(String [] args){ MyRunnable r = new MyRunnable(); Thread t1 = new Thread(r); Thread t2 = new Thread(r); t1.start(); t2.start(); }
class MyRunnable implements Runnable{ private int tickets =10; @Override public void run() { while (tickets>0) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } tickets--; System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets); } } }
class MyRunnable implements Runnable{ private int tickets =10; @Override public void run() { synchronized(this){ while (tickets>0) { tickets--; System.out.println(Thread.currentThread().getName()+"卖了一张票,剩余票数:"+tickets); } } } }
原文地址:http://blog.csdn.net/u013670933/article/details/44517115