main
?方法)。在Java中有两类线程:User Thread(用户线程)、Daemon Thread(守护线程)?
?
Thread
?的子类, 该子类应重写?Thread
?类的?run
?方法:class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
?然后,下列代码会创建并启动一个线程:
PrimeThread p = new PrimeThread(143); p.start();
?另一种方法是声明实现?Runnable
?接口的类。该类然后实现?run
?方法:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } }
?然后,下列代码会创建并启动一个线程:
PrimeRun p = new PrimeRun(143); new Thread(p).start();
?每个线程都有一个标识名,多个线程可以同名。如果线程创建时没有指定标识名,就会为其生成一个新名称。
?
三、线程的状态控制
?线程控制的基本方法有:
public class TestSync implements Runnable { Timer timer = new Timer(); public static void main(String[] args) { TestSync test = new TestSync(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.setName( "t1"); t2.setName( "t2"); t1.start(); t2.start(); } public void run() { timer.add(Thread. currentThread().getName()); } } class Timer { private static int num = 0; public void add(String name) { num++; System. out.println(name + ", 你是第" + num + "个使用timer的线程" ); } }?我们期望的结果是:
synchronize(this){ num++; System. out.println(name + ", 你是第" + num + "个使用timer的线程" ); }?2.放在方法声明中,表示整个方法为同步方法:
public synchronized void add(String name) { num++; System. out.println(name + ", 你是第" + num + "个使用timer的线程" ); }?
/** * 生产者消费者 * 举例:生产窝头,消费窝头 * @author Yuwl */ public class TestProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start(); new Thread(c).start(); } } /** * 窝头 */ class WoTou { int id; WoTou(int id){ this. id = id; } public String toString() { return "WoTou id=" + id ; } } /** * 篮子-栈的实现[先进后出] */ class SyncStack { int index = 0; WoTou []arrWt = new WoTou[6]; public synchronized void push(WoTou wt){ while( index == arrWt. length){ //如果篮子满了 try { this.wait(); //让当前线程进入当前对象的wait pool } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); //唤醒其它正在等待的线程 arrWt[ index] = wt; index++; } public synchronized WoTou take(){ while( index == 0){//如果篮子空了 try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notifyAll(); index--; return arrWt[ index]; } } /** * 生产者 */ class Producer implements Runnable { SyncStack ss; Producer(SyncStack ss){ this. ss = ss; } public void run(){ for( int i=0; i<20; i++){ WoTou wt = new WoTou(i); ss.push(wt); System. out.println( "生产了 "+wt); try { Thread. sleep((long)(Math.random()*200)); } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 消费者 */ class Consumer implements Runnable { SyncStack ss; public Consumer(SyncStack ss) { this. ss = ss; } public void run(){ for( int i=0; i<20; i++){ WoTou wt = ss.take(); System. out.println( "消费了 "+wt); try { Thread. sleep((long)(Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } } } }?其中一次的运行结果:
生产了 WoTou id=0 消费了 WoTou id=0 生产了 WoTou id=1 生产了 WoTou id=2 消费了 WoTou id=2 生产了 WoTou id=3 生产了 WoTou id=4 生产了 WoTou id=5 生产了 WoTou id=6 生产了 WoTou id=7 消费了 WoTou id=7 生产了 WoTou id=8 消费了 WoTou id=8 生产了 WoTou id=9 生产了 WoTou id=10 消费了 WoTou id=9 消费了 WoTou id=10 生产了 WoTou id=11 生产了 WoTou id=12 消费了 WoTou id=11 生产了 WoTou id=13 消费了 WoTou id=12 消费了 WoTou id=13 生产了 WoTou id=14 消费了 WoTou id=14 生产了 WoTou id=15 生产了 WoTou id=16 消费了 WoTou id=15 消费了 WoTou id=16 生产了 WoTou id=17 消费了 WoTou id=17 生产了 WoTou id=18 消费了 WoTou id=18 生产了 WoTou id=19 消费了 WoTou id=19 消费了 WoTou id=6 消费了 WoTou id=5 消费了 WoTou id=4 消费了 WoTou id=3 消费了 WoTou id=1?
原文:http://yuwenlin.iteye.com/blog/2311611