1. 程式人生 > >accp8.0轉換教材第1章多線程理解與練習

accp8.0轉換教材第1章多線程理解與練習

獲取 stack 創建 exc 同步方法 emp 默認 一個 ack

一.單詞部分:

①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());

}
}
}

再創建main方法類去掉用就行了

package oneOne;

public class testone {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnableone my=new MyRunnableone();
MyRunnableone my1=new MyRunnableone();
my.start();
my1.start();
}

}

2.使用實現Runnable接口方式創建線程

同第一個先創建實現類:

package oneTwo;

public class MyRunnabletwo implements Runnable{

public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,來自線程"+Thread.currentThread().getName());

}
}


}

再main方法:

package oneTwo;

public class testtwo {

/**
* @param args
*/
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 {

/**
* @param args
*/
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 {

/**
* @param args
*/
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 {

/**
* @param args
*/
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 {

/**
* @param args
*/
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
public void run() {
// 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+"張票!");
}
}
}

}

再創建main接口類:

package oneSeven;

public class testseven {

/**
* @param args
*/
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()方法

accp8.0轉換教材第1章多線程理解與練習