《Java编程那点事儿》读书笔记(七)——多线程
1.继承Thread类
通过编写新的类继承Thread类可以实现多线程,其中线程的代码必须书写在run方法内部或者在run方法内部进行调用。
public class NewThread extends Thread { private int ThreadNum; public NewThread(int ThreadNum){ this.ThreadNum = ThreadNum; } public void run(){ try{ for(int i = 0;i < 10;i ++){ Thread.sleep(1000); System.out.println("running Thread"+ThreadNum+":"+i); } }catch(Exception e){ } } }
上述代码定义了新线程NewThread,并在run中实现输出十个数的功能。用以下代码在main函数中调用:
NewThread nt = new NewThread(1); nt.start(); NewThread nt2 = new NewThread(2); nt2.start();
得到的结果如下:
running Thread1:0 running Thread2:0 running Thread1:1 running Thread2:1 running Thread1:2 running Thread2:2 running Thread1:3 running Thread2:3 running Thread1:4 running Thread2:4 running Thread1:5 running Thread2:5 running Thread1:6 running Thread2:6 running Thread1:7 running Thread2:7 running Thread1:8 running Thread2:8 running Thread1:9 running Thread2:9
可以看到启动的两个线程并行运行。
2.实现Runnable接口(Java.lang.Runnable)
public class MyRunnable implements Runnable { private int ThreadNum; public MyRunnable(int ThreadNum){ this.ThreadNum = ThreadNum; } public void run(){ try{ for(int i = 0;i < 10;i ++){ Thread.sleep(1000); System.out.println("running Thread"+ThreadNum+":"+i); } }catch(Exception e){ } } }
在main中调用接口:
MyRunnable mr = new MyRunnable(1); Thread t = new Thread(mr); MyRunnable mr2 = new MyRunnable(2); Thread t2 = new Thread(mr2); t.start(); t2.start();
3.Timer & TimerTask组合实现多线程
public class TimerAndTimerTask extends TimerTask{ private String s; public TimerAndTimerTask(String s){ this.s = s; } public void run(){ try{ for(int i = 0;i < 10;i ++){ Thread.sleep(1000); System.out.println("running Thread"+s+":"+i); } }catch(Exception e){ } } }
在main函数中创建线程代码如下:
Timer t = new Timer(); Timer t2 = new Timer(); TimerAndTimerTask tatt = new TimerAndTimerTask("1"); TimerAndTimerTask tatt2 = new TimerAndTimerTask("2"); t.schedule(tatt, 0); t2.schedule(tatt2, 0);
上述代码中的要用两个Timer启动不同的线程,它们才能同时运行。如果只用一个Timer,则一次只能启动一个线程。
上述中的schedule方法一共有四种多态:
public void schedule(TimeTask task, Date time) //在2009年10月1日10点0分0秒启动该线程或超过该时间也启动线程 Date d = new Date(2009-1900,10-1,1,10,0,0); t.schedule(task,d);
public void schedule(TimerTask task,Date firstTime,long period) //到达或者超过2009年10月1日10点时候每隔20000ms就启动一次线程,这种方式会重复触发线程 Date d = new Date(2009-1900,10-1,1,10,0,0); t.schedule(task,d,20000);
public void schedule(TimerTask task,long delay) //执行启动代码1000ms后启动线程 t.schedule(task,1000);
//在delay ms后启动线程,并且每隔period ms启动一次 public void schedule(TimerTask task,long delay,long period)
在eclipse里面试了一下最后一种,会不断的输出0~9这10个数,因为用的Timer只有一个,但是每过1s就触发一次线程,所以不停的有线程需要执行。
4.互斥
synchronized,修饰方法或代码块,表示如果两个或者以上的线程同时执行该代码段时,如果一个线程已经开始执行该代码段,则另外一个线程必须等待这个线程执行完这段代码后才能执行。
public class Toilet { public synchronized void enter(String name){ System.out.println(name+" enters the toilet!"); try{ Thread.sleep(2000); }catch(Exception e){} System.out.println(name + " has left the toilet!"); } } public class Human extends Thread{ private Toilet t; private String s; public Human(String s,Toilet t){ this.s = s; this.t = t; start(); } public void run(){ t.enter(s); } }
在main中创建Human线程的代码如下,一共创建了三个Human线程:
Toilet t = new Toilet(); Human t1 = new Human("Ann", t); Human t2 = new Human("Jeff", t); Human t3 = new Human("Joe", t);
在上述Toilet类中有一段互斥的代码,输出当前进入厕所的人,并且在1s后离开。
在类Human中,在构造函数里面开启进程,所以每当new一个Human类就相当于开启了一个线程,但是会不会立即执行run函数要看系统中是否已经有在跑的Human线程,因为run函数里面调用了Toilet类中的enter函数,这个函数是互斥的,一次只能有一个线程执行。
程序两次执行的结果如下:
Jeff enters the toilet! Jeff has left the toilet! Joe enters the toilet! Joe has left the toilet! Ann enters the toilet! Ann has left the toilet!
Ann enters the toilet! Ann has left the toilet! Jeff enters the toilet! Jeff has left the toilet! Joe enters the toilet! Joe has left the toilet!
可以看到,线程执行的顺序并不是固定的,但是同一时刻一定只有一个线程可以执行enter这段代码。
5.同步
主要涉及两个函数
wait():使调用该方法的线程进入休眠
notify():使调用该方法的线程被唤醒
6.线程优先级
MAX_PRIORITY //最高优先级 NORM_PRIORITY //普通(默认)优先级 MIN_PRIORITY //最低优先级
如果上述main函数中通过调用MyRunnable类实现多线程的main函数中的程序改为如下:
MyRunnable mr = new MyRunnable(1); Thread t = new Thread(mr); t.setPriority(Thread.MIN_PRIORITY); MyRunnable mr2 = new MyRunnable(2); Thread t2 = new Thread(mr2); t2.setPriority(Thread.NORM_PRIORITY); MyRunnable mr3 = new MyRunnable(3); Thread t3 = new Thread(mr3); t3.setPriority(Thread.MAX_PRIORITY); t.start(); t2.start(); t3.start();
但是我没有得到书上的结果,大部分情况下是线程3先执行,但有时候也会出现线程2先执行的情况,某次执行的结果如下:
running Thread3:0 running Thread1:0 running Thread2:0 running Thread3:1 running Thread2:1 running Thread1:1 running Thread3:2 running Thread1:2 running Thread2:2 running Thread3:3 running Thread1:3 running Thread2:3 running Thread3:4 running Thread1:4 running Thread2:4 running Thread3:5 running Thread1:5 running Thread2:5 running Thread3:6 running Thread1:6 running Thread2:6 running Thread3:7 running Thread1:7 running Thread2:7 running Thread3:8 running Thread1:8 running Thread2:8 running Thread3:9 running Thread1:9 running Thread2:9
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。