我上課整理出來的多線程的例子,大家看看。包括:
- 使用Thread建立線程;
- 使用Runnable建立線程;
- 執行個體:讓人能夠同時說話和開車;
- 執行個體:類比產生者和消費者。
1、使用Thread實現多線程
範例程式碼:
package ch18;</p><p>public class ThreadTest extends Thread{<br />public boolean b=false;<br />public static void main(String[] args) {<br />ThreadTest thread = new ThreadTest();<br />thread.setName("child");<br />thread.start();</p><p>try {<br />thread.join();<br />} catch (InterruptedException e1) {<br />// TODO Auto-generated catch block<br />e1.printStackTrace();<br />}</p><p>ThreadTest thread1 = new ThreadTest();<br />thread1.setName("child-------");<br />thread1.start();</p><p>for(int i=0;i<5;i++){<br />try {<br />sleep(100);<br />} catch (InterruptedException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br />System.out.println(currentThread().getName()+":"+i);<br />}</p><p>thread.b = true;<br />thread1.b = true;<br />}</p><p>public void run(){<br />for(int i=0;i<10;i++){<br />try {<br />sleep(100);<br />} catch (InterruptedException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br />System.out.println(currentThread().getName()+":"+i);<br />if(b)<br />break;<br />}<br />}<br />}<br />
2、使用Runnable實現多線程
範例程式碼:
package ch18;</p><p>public class RunnableTest implements Runnable{</p><p>public static void main(String[] args) {<br />Runnable r1 = new RunnableTest();<br />Thread thread = new Thread(r1,"t1");<br />thread.start();<br />thread.setPriority(9);</p><p>Thread thread2 = new Thread(r1,"t2");<br />thread2.start();<br />thread2.setPriority(2);<br />}</p><p>public void run(){<br />for(int i=0;i<100;i++){<br />System.out.println(Thread.currentThread().getName()+":"+i);<br />}<br />}<br />}<br />
3、執行個體:讓人能夠同時說話和開車
範例程式碼:
package ch19;</p><p>import static java.lang.System.out;</p><p>public class Person implements Runnable{</p><p>int speakNo=0;<br />int driveNo=0;</p><p>private boolean canStop=false; // 是否停止線程</p><p>public static void main(String[] args) {<br />Person person = new Person();</p><p>Thread t1 = new Thread(person,"speak"); // 第二個參數給出線程的名字<br />Thread t2 = new Thread(person,"drive");</p><p>t1.setPriority(8);<br />t2.setPriority(4);</p><p>t1.start();<br />t2.start();</p><p>try {<br />Thread.currentThread().sleep(1000);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}</p><p>person.setCanStop(true);<br />}</p><p>public void run(){<br />while(true){<br />String name = Thread.currentThread().getName(); // 得到當前線程<br />的名字<br />if(name.equals("speak")){<br />speak();<br />}else{<br />drive();<br />}<br />if(canStop){<br />break;<br />}<br />}<br />}</p><p>public void drive(){<br />out.println("正在--------------開車!"+driveNo++);<br />//try {<br />//Thread.currentThread().sleep(5);<br />//} catch (InterruptedException e) {<br />//e.printStackTrace();<br />//}<br />}</p><p>public void speak(){<br />out.println("正在說話!"+speakNo++);<br />//try {<br />//Thread.currentThread().sleep(5);<br />//} catch (InterruptedException e) {<br />//e.printStackTrace();<br />//}<br />}</p><p>public boolean isCanStop() {<br />return canStop;<br />}</p><p>public void setCanStop(boolean canStop) {<br />this.canStop = canStop;<br />}</p><p>}<br />
4、執行個體:類比產生者消費者模型
範例程式碼:
package ch19;</p><p>import static java.lang.System.out;</p><p>// 第一步:實現Runnable介面,並且實現run方法<br />public class Factory implements Runnable {<br />// 第二步:定義表示庫存變數quantity,quantity的值會影響生產和消費這兩個方法的執<br />行,有產品才可以消費,倉庫沒有滿才可以生產<br />// 庫存,初始為0,最大為10<br />private int quantity = 0;<br />// 第六步:建立成員變數canStop控制線程的結束,主線程中修改控制變數的值,在生產和<br />消費線程中判斷該變數的值,然後結束線程。</p><p>private boolean canStop = false;</p><p>public int getQuantity() {<br />return quantity;<br />}</p><p>public void setQuantity(int quantity) {<br />this.quantity = quantity;<br />}</p><p>public boolean isCanStop() {<br />return canStop;<br />}a</p><p>public void setCanStop(boolean canStop) {<br />this.canStop = canStop;<br />}</p><p>// 第四步:建立工廠對象,然後建立生產和消費的線程並啟動<br />public static void main(String args[]) {<br />Factory factory = new Factory();</p><p>Thread t1 = new Thread(factory, "producer1");<br />Thread t2 = new Thread(factory, "producer2");<br />Thread t3 = new Thread(factory, "consumer1");<br />Thread t4 = new Thread(factory, "consumer2");</p><p>t1.start();<br />t2.start();<br />t3.start();<br />t4.start();</p><p>try {<br />Thread.currentThread().sleep(300);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}</p><p>factory.setCanStop(true);<br />}</p><p>// 第五步:實現run方法,根據當前線程的任務去調用相應的方法,或者去生產 或者消費<br />public void run() {<br />String threadName = Thread.currentThread().getName();<br />while (true) {<br />if (threadName.startsWith("producer")) {<br />produce();<br />} else {<br />consume();<br />}<br />if(canStop)<br />break;<br />}<br />}</p><p>// 第三步:定義消費和生產的方法<br />public synchronized void consume() {<br />// 判斷是否有商品<br />while (quantity == 0) {<br />try {<br />wait();<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}</p><p>// 消費商品<br />quantity--;<br />String threadName = Thread.currentThread().getName();<br />out.println(threadName + "消費了一個商品!");<br />try {<br />Thread.currentThread().sleep(50);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}</p><p>notifyAll();<br />}</p><p>// 產生商品<br />public synchronized void produce() {<br />// 判斷倉庫是否已經滿了,如果滿了,就等待<br />while (quantity == 10) {<br />try {<br />wait();<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}<br />}</p><p>// 生產<br />quantity++;<br />String threadName = Thread.currentThread().getName();<br />out.println(threadName + "生產了一個商品!");<br />try {<br />Thread.currentThread().sleep(50);<br />} catch (InterruptedException e) {<br />e.printStackTrace();<br />}</p><p>notifyAll();<br />}<br />}<br />