第一部分:理论知识学习部分
第14章 并发
⚫ 线程的概念
⚫ 中断线程
⚫ 线程状态
⚫ 多线程调度
⚫ 线程同步
1.程序与进程的概念
1.1程序是一段静态的代码,它是应用程序执行的蓝 本。
1.2进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程。
1.3操作系统为每个进程分配一段独立的内存空间和系统资源,包括:代码数据以及堆栈等资源。每一个进程的内部数据和状态都是完全独立的。
1.4多任务操作系统中,进程切换对CPU资源消耗较大。
2.多线程的概念
2.1多线程是进程执行过程中产生的多条执行线索。
2.2线程是比进程执行更小的单位。
2.3线程不能独立存在,必须存在于进程中,同一进 程的各线程间共享进程空间的数据。
2.4每个线程有它自身的产生、存在和消亡的过程, 是一个动态的概念。
2.5多线程意味着一个程序的多行语句可以看上去几 乎在同一时间内同时运行。
2.6线程创建、销毁和切换的负荷远小于进程,又称为轻量级进程(lightweight process)。
2.7Java实现多线程有两种途径:
2.7.1创建Thread类的子类
2.7.2在程序中定义实现Runnable接口的类
3.线程的终止
3.1 当线程的run方法执行方法体中最后一条语句后, 或者出现了在run方法中没有捕获的异常时,线 程将终止,让出CPU使用权。
3.2调用interrupt()方法也可终止线程。 void interrupt() –
3.2.1向一个线程发送一个中断请求,同时把这个线 程的“interrupted”状态置为true。
3.2.2若该线程处于blocked 状态, 会抛出 InterruptedException。
4.测试线程是否被中断的方法
Java提供了几个用于测试线程是否被中断的方法。
⚫ static boolean interrupted() – 检测当前线程是否已被中断, 并重置状态 “interrupted”值为false。
⚫ boolean isInterrupted() – 检测当前线程是否已被中断, 不改变状态 “interrupted”值 。
5.线程的状态
⚫ 利用各线程的状态变换,可以控制各个线程轮流 使用CPU,体现多线程的并行性特征。
⚫ 线程有如下7种状态: ➢New (新建) ➢Runnable (可运行) ➢Running(运行) ➢Blocked (被阻塞) ➢Waiting (等待) ➢Timed waiting (计时等待) ➢Terminated (被终止)
6.守护线程
⚫ 守护线程的惟一用途是为其他线程提供服务。例 如计时线程。
⚫ 若JVM的运行任务只剩下守护线程时,JVM就退 出了。
⚫ 在一个线程启动之前,调用setDaemon方法可 将线程转换为守护线程(daemon thread)。
例如: setDaemon(true);
第二部分:实验部分——线程技术
实验时间 2017-12-8
1、实验目的与要求
(1) 掌握线程概念;
(2) 掌握线程创建的两种技术;
(3) 理解和掌握线程的优先级属性及调度方法;
(4) 掌握线程同步的概念及实现技术;
2、实验内容和步骤
实验1:测试程序并进行代码注释。
测试程序1:
1.在elipse IDE中调试运行ThreadTest,结合程序运行结果理解程序;
2.掌握线程概念;
3.掌握用Thread的扩展类实现线程的方法;
4.利用Runnable接口改造程序,掌握用Runnable接口创建线程的方法。
1 class Lefthand extends Thread { 2 public void run() 3 { 4 for(int i=0;i<=5;i++) 5 { System.out.println("You are Students!"); 6 try{ sleep(500); } 7 catch(InterruptedException e) 8 { System.out.println("Lefthand error.");} 9 } 10 } 11 } 12 class Righthand extends Thread { 13 public void run() 14 { 15 for(int i=0;i<=5;i++) 16 { System.out.println("I am a Teacher!"); 17 try{ sleep(300); } 18 catch(InterruptedException e) 19 { System.out.println("Righthand error.");} 20 } 21 } 22 } 23 public class ThreadTest { 24 static Lefthand left; 25 static Righthand right; 26 27 28 public static void main(String[] args) 29 { left=new Lefthand(); 30 right=new Righthand(); 31 left.start(); 32 right.start(); 33 } 34 }
改:
1 class Lefthand implements Runnable { 2 public void run() 3 { 4 for(int i=0;i<=5;i++) 5 { System.out.println("You are Students!"); 6 try{ Thread.sleep(500); } 7 catch(InterruptedException e) 8 { System.out.println("Lefthand error.");} 9 } 10 } 11 } 12 class Righthand implements Runnable { 13 public void run() 14 { 15 for(int i=0;i<=5;i++) 16 { System.out.println("I am a Teacher!"); 17 try{ Thread.sleep(300); } 18 catch(InterruptedException e) 19 { System.out.println("Righthand error.");} 20 } 21 } 22 } 23 24 public class fufj { 25 26 static Thread left; 27 static Thread right; 28 29 public static void main(String[] args) 30 { 31 Runnable rleft = new Lefthand(); 32 Runnable rright = new Righthand(); 33 left = new Thread(rleft); 34 right = new Thread(rright); 35 left.start(); 36 right.start(); 37 } 38 39 } 40
测试程序2:
1.在Elipse环境下调试教材625页程序14-1、14-2 、14-3,结合程序运行结果理解程序;
2.在Elipse环境下调试教材631页程序14-4,结合程序运行结果理解程序;
3.对比两个程序,理解线程的概念和用途;
4.掌握线程创建的两种技术。
1 package bounce; 2 3 import java.awt.geom.*; 4 5 /** 6 * 弹球从矩形的边缘上移动和弹出的球 7 * @version 1.33 2007-05-17 8 * @author Cay Horstmann 9 */ 10 public class Ball 11 { 12 private static final int XSIZE = 15; 13 private static final int YSIZE = 15; 14 private double x = 0; 15 private double y = 0; 16 private double dx = 1; 17 private double dy = 1; 18 19 /** 20 * 将球移动到下一个位置,如果球击中一个边缘,则向相反的方向移动。 21 */ 22 public void move(Rectangle2D bounds) 23 { 24 x += dx; 25 y += dy; 26 if (x < bounds.getMinX()) 27 { 28 x = bounds.getMinX(); 29 dx = -dx; 30 } 31 if (x + XSIZE >= bounds.getMaxX()) 32 { 33 x = bounds.getMaxX() - XSIZE; 34 dx = -dx; 35 } 36 if (y < bounds.getMinY()) 37 { 38 y = bounds.getMinY(); 39 dy = -dy; 40 } 41 if (y + YSIZE >= bounds.getMaxY()) 42 { 43 y = bounds.getMaxY() - YSIZE; 44 dy = -dy; 45 } 46 } 47 48 /** 49 * 获取当前位置的球的形状。 50 */ 51 public Ellipse2D getShape() 52 { 53 return new Ellipse2D.Double(x, y, XSIZE, YSIZE); 54 } 55 }