accp8.0转换教材第1章多线程理解与练习
一.單詞部分:
①process進(jìn)程 ②current當(dāng)前的③thread線程④runnable可獲取的
⑤interrupt中斷⑥join加入⑦yield產(chǎn)生⑧synchronize同時(shí)發(fā)生
二.預(yù)習(xí)部分
1.線程與進(jìn)程的區(qū)別:
進(jìn)程是系統(tǒng)運(yùn)行程序的基本單位
線程是進(jìn)程中執(zhí)行運(yùn)算的最小單位
2.說(shuō)明創(chuàng)建線程的方式有哪兩種
①繼承thread類(lèi)
②實(shí)現(xiàn)Runnable接口
3.線程的生命周期可分為幾個(gè)階段,各是什么階段
五個(gè)階段:①創(chuàng)建②就緒③運(yùn)行④阻塞⑤死亡
4.使用線程的什么方法可以設(shè)置線程的休眠,線程的強(qiáng)制執(zhí)行,線程的禮讓
分別為:sleep(),join(),yield()
5.什么情況下需要進(jìn)行線程的同步,線程的同步有幾種方式
當(dāng)訪問(wèn)沖突時(shí)需要進(jìn)行
兩種方式:①同步方法②同步代碼塊
三.練習(xí)部分
1.使用繼承Thread類(lèi)的方法創(chuàng)建線程,顯示相應(yīng)內(nèi)容
首先創(chuàng)建一個(gè)線程類(lèi):
package oneOne;
public class MyRunnableone extends Thread{
public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,來(lái)自線程"+Thread.currentThread().getName());
}
}
}
再創(chuàng)建main方法類(lèi)去掉用就行了
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.使用實(shí)現(xiàn)Runnable接口方式創(chuàng)建線程
同第一個(gè)先創(chuàng)建實(shí)現(xiàn)類(lèi):
package oneTwo;
public class MyRunnabletwo implements Runnable{
public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,來(lá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.使用多線程模擬多人徒步爬山
先創(chuàng)建繼承或者實(shí)現(xiàn)類(lèi)(我這里用了繼承):
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()+"到達(dá)終點(diǎn)!");
}
}
再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("**********開(kāi)始爬山*********");
old.start();
young.start();
child.start();
}
}
4.顯示,設(shè)置線程優(yōu)先級(jí)
先繼承或者實(shí)現(xiàn)類(lèi):
package oneFour;
public class MyRunnablefour extends Thread{
public void run() {
Thread.currentThread().setPriority(1);
System.out.println("子線程名:"+Thread.currentThread().getName()+",優(yōu)先級(jí):"+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("*************顯示默認(rèn)優(yōu)先級(jí)********");
System.out.println("主線程名:"+Thread.currentThread().getName()+",優(yōu)先級(jí):"+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("***********修改默認(rèn)優(yōu)先級(jí)后***********");
//myf.setPriority(1);
System.out.println("主線程名:"+Thread.currentThread().getName()+",優(yōu)先級(jí):"+Thread.currentThread().getPriority());
//System.out.println("子線程名:"+MyRunnablefour.currentThread().getName()+",優(yōu)先級(jí):"+MyRunnablefour.currentThread().getPriority());
}
}
5.模擬叫號(hào)看病
先繼承或?qū)崿F(xiàn)類(lèi):
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("特需號(hào):"+i+"號(hào)病人在看病!");
}
}
}
再main:
package oneFive;
public class testfive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//MyRunnablefive pertail=new MyRunnablefive("特需號(hào)", 1000);
Thread temp=new Thread(new MyRunnablefive("特需號(hào)", 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("普通號(hào):"+i+"號(hào)病人在看病");
}
}
}
6.多線程模擬 接力賽跑
先創(chuàng)建繼承或者實(shí)現(xiàn)類(lèi):
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("進(jìn)來(lái)了");
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接口類(lèi):
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)+"號(hào)選手").start();
}
}
}
7.多線程模擬網(wǎng)絡(luò)購(gòu)票
桃跑跑,張票票,黃牛黨,共同搶十張票,限制黃牛黨只能搶一次票
先創(chuàng)建繼承或者實(shí)現(xiàn)類(lèi):
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("進(jìn)來(lái)了");
while (!flag) {
synchronized (this){
//System.out.println("進(jìn)來(lái)了");
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+"張票!");
}
}
}
}
再創(chuàng)建main接口類(lèi):
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();
}
}
?四:總結(jié):
1.Thread類(lèi)中的方法實(shí)現(xiàn)對(duì)線程對(duì)象的操作
①調(diào)整線程的優(yōu)先級(jí)
②線程睡眠sleep()
③線程的強(qiáng)制運(yùn)行join()
④線程禮讓yield()
2.多線程允許程序員編寫(xiě)可最大利用CPU的高效程序
3.兩種方式創(chuàng)建線程:
①聲明一個(gè)繼承了Thread類(lèi)的子類(lèi)并實(shí)現(xiàn)Thread類(lèi)的run()方法
②聲明一個(gè)實(shí)現(xiàn)Runnable接口的類(lèi),然后實(shí)現(xiàn)run()方法
?原文在博客園有需要可以聯(lián)系扣扣:2265682997
轉(zhuǎn)載于:https://www.cnblogs.com/a782126844/p/7094872.html
總結(jié)
以上是生活随笔為你收集整理的accp8.0转换教材第1章多线程理解与练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 程序媛计划——python数据库
- 下一篇: 动态规划 所有题型的总结