一.單詞部分:
①process進程②current當前的③thread線程④runnable可獲取的
⑤interrupt中斷⑥join加入⑦yield產生⑧synchronize同時發生
二.預習部分
1.執行緒與進程的差異:
進程是系統執行程式的基本單位
執行緒是進程中執行運算的最小單位
2 .說明創建線程的方式有哪兩種
①繼承thread類別
②實作Runnable介面
3.線程的生命週期可分為幾個階段,各是什麼階段
五個階段:①創建②就緒③運行④阻塞⑤死亡
4.使用線程的什麼方法可以設定線程的休眠,線程的強制執行,線程的禮讓
分別為:sleep(),join(),yield()
5.什麼情況下需要進行執行緒的同步,執行緒的同步有幾種方式
當存取衝突時需要進行
兩種方式:①同步方法②同步程式碼區塊
#三.練習部分
1.使用繼承Thread類別的方法建立線程,顯示對應內容
首先建立一個執行緒類別:
package oneOne;
public class MyRunnableone extends Thread{
## public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,來自線程"+Thread.currentThread().getName());
}
}
}
* @參數參數
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnableone my=new MyRunnableone();
MyRunnableone my1 =new MyRunnableone();
my.start();
my1.start();
}
## public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,來自線程"+Thread.currentThread().getName());
}
}
再main方法:
package oneTwo;
public class testtwo {
/**
* @參數參數*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnabletwo my=new MyRunnabletwo();
MyRunnabletwo my1=new MyRunnabletwo();
Thread tr=new Thread(my);
Thread tr1=new Thread(my1);
tr.start();
tr1.start();
}
}
3.使用多執行緒模擬多人徒步爬山
#先建立繼承或實作類別(我這裡用了繼承):
package oneThree;
public class MyRunnablethree extends Thread{
private int time;public int num=0;
public MyRunnablethree(String name,int time,int kio) {
super(name);
this.time=time;
this.num=kio*1000/100;
}
public void run() {
while (num>0) {
try {
Thread.sleep(this.time);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace ();
}
System.out.println(Thread.currentThread().getName()+"爬完100公尺! ");
num--;
}
System.out.println(Thread.currentThread().getName()+"到達終點!");
}
##}
再main方法:
package oneThree;
public class testthree {
/**
* @參數參數*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnablethree young=new MyRunnablethree("年輕人", 500, 1);
MyRunnablethree old=new MyRunnablethree( "老人", 1500, 1);
MyRunnablethree child=new MyRunnablethree("小孩", 600, 1);
System.out.println("**********開始爬山*********");
old.start();
young.start();
child.start();
}
}
4.顯示,設定執行緒優先權
先繼承或實作類別:
package oneFour;
public class MyRunnablefour extends Thread{
public void run() { Thread.currentThread().setPriority(1);
System.out.println("子執行緒名稱:"+Thread.currentThread().getName()+",優先權:"+Thread.currentThread().getPriority());
}
}
再main:
package oneFour;
public class testfour {
/**
* @參數參數
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnablefour myf=new MyRunnablefour();
myf.start();
System.out.println("*************顯示預設優先權********");
System.out .println("主執行緒名稱:"+Thread.currentThread().getName()+",優先權:"+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10 );
System.out.println("***********修改預設優先權後***********");
//myf.setPriority (1);
System.out.println("主執行緒名稱:"+Thread.currentThread().getName()+",優先權:"+Thread.currentThread().getPriority());
//System.out.println("子執行緒名稱:"+MyRunnablefour.currentThread().getName()+",優先權:"+MyRunnablefour.currentThread().getPriority());
}
}
5.模擬叫號看病
先繼承或實作類別:
package oneFive;
public class MyRunnablefive extends Thread{
private int time;
//public int pertail=0;
public MyRunnablefive(String common,int time) {
super(common);
this. time=time;
}
public void run() {
Thread.currentThread().setPriority(8);
for(int i=1;i<=10;i++ ){
try {
Thread.sleep(this.time);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("特需號:"+i+"號病人在看病! ");
}
}
}
再main:
package oneFive;
public class testfive {
/**
* @參數參數
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//MyRunnablefive pertail=new MyRunnablefive("特需編號", 1000);
Thread temp=new Thread(new MyRunnablefive("特需編號", 400));
temp.start();
Thread.currentThread().setPriority (4);
for(int i=1;i<=50;i++){
# if(i==11){
try {
temp.join ();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
try {
Thread.sleep (200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("普通號:"+i+"號病人在看病");
}
}
# }
6.多執行緒模擬接力賽跑
先建立繼承或實作類別:
package oneSix;
public class runSix implements Runnable{
private int meters=1000;
public runSix(){
}
@Override
public void run() {
// TODO Auto-generated method stub
//System.out.println("進來了");
while (true) {
//type type = (type) true.nextElement();
synchronized (this) {
if(meters<=100){
break;
# }
System.out.println(Thread.currentThread().getName()+"拿到接力棒了! ");
for (int i = 0; i < 100; i+=10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"跑了"+(i+10 )+"米!");
}
meters-=100;
}
# }
}
}
再main介面類別:
package oneSix;
public class testsix {
/**
* @參數參數
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
runSix ru=new runSix();
for (int i = 0; i < 5; i++) {
new Thread(ru,(i+1) +"號選手").start();
}
}
}
#7.多執行緒模擬網路購票
桃子跑跑,張票票,黃牛黨,共同搶十張票,限制黃牛黨只能搶一次票
先創建繼承或實現類別:
package oneSeven;
public class siteSeven implements Runnable{
private int count=10;
private int num=0;
private boolean flag=false;
@Override
. () {
// TODO Auto-generated method stub
//System.out.println("進來了");
while (!flag) {
synchronized (this) {
//System.out.println("進來了");
if(count<=0){
flag=true;
return;
}
num++;
count--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace( );
}
String name=Thread.currentThread().getName();
if(name.equals("黃牛黨")){
System.out.println(name+"搶到第"+num+"張票,剩餘"+count+"張票! ");
break;
}
System.out.println(name+"搶到第"+num+"張票,剩餘"+count+"張票! #package oneSeven;
public class testseven {
/**
*/
public static void main(String[] args) {// TODO Auto- generated method stub
siteSeven si=new siteSeven();Thread per1=new Thread(si,"大東");
Thread yellow=new Thread(si,"黃牛黨");Thread per2=new Thread(si,"啟圳");
per1.start(); yellow.start();
per2.start();
}
}
四:總結:
1.Thread類別中的方法實作線程物件的操作
①調整執行緒的優先權
②線程睡眠sleep()
③線程的強制運行join()
#④線程禮讓yield()
2.多執行緒允許程式設計師編寫可最大利用CPU的高效程式
3.兩種方式建立執行緒:
①宣告一個繼承了Thread類別的子類別並實作Thread類別的run()方法
②聲明實作Runnable介面的類,然後實作run()方法
以上是多線程的理解與使用介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!