多线程机制 7.1线程的概念 7.2 Runnable.接口与 Thread类 7.3线程的控制与调度 74线程的同步机制
多线程机制 7.1 线程的概念 7.2 Runnable接口与Thread类 7.3 线程的控制与调度 7.4 线程的同步机制
7.1线程的概念 程序、进程和线程 线程的生命周期与线程的状态 创建、可运行、运行中、阻塞、死亡 ·日常生活中的程序、进程和线程 程序:每学期的课程表 进程:每学期的教学活动 线程:每门课的教学过程
7.1 线程的概念 • 程序、进程和线程 • 线程的生命周期与线程的状态 创建、可运行、运行中、阻塞、死亡 • 日常生活中的程序、进程和线程 程序:每学期的课程表 进程:每学期的教学活动 线程:每门课的教学过程
Runnable接口与 Thread类 Runnable接 · Thread类 ·例7.1继承 Thread类创建线程 ·例72实现 Runnable.接口创建线程 两种创建线程方法的比较 线程组
Runnable接口与Thread类 • Runnable接口 • Thread类 • 例7.1 继承Thread类创建线程 • 例7.2 实现Runnable接口创建线程 • 两种创建线程方法的比较 • 线程组
继承 Thread类创建线程 public class Thread1 extends Thread int k =0 public Thread 1(String name, int k) super(name) this k= k
继承Thread类创建线程 public class Thread1 extends Thread { int k=0; public Thread1(String name,int k) { super(name); this.k = k; }
public void runo ∥盖run方法的线程体 inti= k System. out. printIn o: System. out. print(getName(+.) While((<50) System. out. print(i+) j+=2 System. out printIn(getName0+end
public void run() //覆盖run方法的线程体 { int i = k; System.out.println(); System.out.print(getName()+": "); while (i<50) { System.out.print(i+" "); i+=2; } System.out.println(getName() +" end!"); }
public static void main(String args) Thread1 t1= new Thread1( Thread1",1) ∥)创建线程对象 Thread 1 t2= new Thread1( Thread2, 2) t1.start ∥动执行线程 t2.start System. out. printIn activeCount="+t2. active Counto
public static void main (String args[]) { Thread1 t1 = new Thread1("Thread1",1); //创建线程对象 Thread1 t2 = new Thread1("Thread2",2); t1.start(); //启动执行线程 t2.start(); System.out.println("activeCount="+t2.activeCount()); } }
例7.1的运行结果 D: \myjava>javac Thread1.java D: Myjava>java thread 1 active Count=3 Thread1:13579111315171921232527 Thread2:246810121416182022242628 30323436384042444648 Thread2end! 2931333537394143454749 Thread1end
例7.1的运行结果 D:\myjava>javac Thread1.java D:\myjava>java Thread1 activeCount=3 Thread1: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 Thread2: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 Thread2 end! 29 31 33 35 37 39 41 43 45 47 49 Thread1 end!
实现 Runnable接口创建线程 public class runnable implements Runnable i int k=0 public Runnable 1 (int k) i this k= k; public void runo i int i=k System. out. printIng while(i<50) i System. out. print(i+ i+=2:
实现Runnable接口创建线程 public class Runnable1 implements Runnable { int k=0; public Runnable1(int k) { this.k = k; } public void run() { int i = k; System.out.println(); while (i<50) { System.out.print(i+" "); i+=2; } }
public static void main(String args) Runnable 1 r1= new Runnable1(1) ∥)创建具有线程体的目标对象 Runnable1 r2 new Runnable 1(2) Thread t1=new Thread(r1) ∥以目标对象创建线程 Thread t2=new Thread(r2) t1.start t2. start
public static void main (String args[]) { Runnable1 r1 = new Runnable1(1); //创建具有线程体的目标对象 Runnable1 r2 = new Runnable1(2); Thread t1=new Thread(r1); //以目标对象创建线程 Thread t2=new Thread(r2); t1.start(); t2.start(); } }
例7.2的运行结果 D: \myjava>javac Runnable.java D: \myjava>java Runnable 1 1357911131517192123 24681012141618202224262830 323436384042444648 25272931333537394143454749
例7.2的运行结果 D:\myjava>javac Runnable1.java D:\myjava>java Runnable1 1 3 5 7 9 11 13 15 17 19 21 23 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 25 27 29 31 33 35 37 39 41 43 45 47 49