面向对象程序设计第10章.ppt

上传人:本田雅阁 文档编号:2264897 上传时间:2019-03-13 格式:PPT 页数:20 大小:212.51KB
返回 下载 相关 举报
面向对象程序设计第10章.ppt_第1页
第1页 / 共20页
面向对象程序设计第10章.ppt_第2页
第2页 / 共20页
面向对象程序设计第10章.ppt_第3页
第3页 / 共20页
亲,该文档总共20页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《面向对象程序设计第10章.ppt》由会员分享,可在线阅读,更多相关《面向对象程序设计第10章.ppt(20页珍藏版)》请在三一文库上搜索。

1、第 10 章,多线程程序设计,线程的定义,进程是一个用来描述处于动态运行状态的应用程序的概念,即一个进程就是一个执行中的程序,每个进程都有一块自己独立的地址空间,并可以包含多个线程。这些线程将共享进程的地址空间及操作系统分配给这个进程的资源。 线程是指进程中的一个执行流,多线程是指在一个进程中同时运行多个不同线程,每个线程分别执行不同的任务。,10.1 创建线程,创建线程的两种方法: 利用Thread类创建线程 利用Runnable接口创建线程,1、利用Thread类创建线程,使用这种方式创建、启动线程的基本过程: 声明一个Thread类的子类,并且覆盖其中的run() 成员方法,并将线程执行

2、的程序代码写在其中。 使用时通过调用Thread类提供的start() 方法间接地使用它。,例:利用Thread类创建线程,public class MyThread_1 extends Thread public void run() ,public class MultiThread_1 public static void main(String args ) Thread t1 = new MyThread_1(); t1.setName(“T1“); Thread t2 = new MyThread_1(); t2.setName(“T2“); t1.start(); t2.star

3、t(); ,2、利用Runnable接口创建线程,使用这种方式创建、启动线程的基本过程: 声明一个实现Runnable接口的类。 以实现Runnable 接口的类对象为参数创建一个Thread 类对象。 调用Thread类对象的start() 方法启动线程。,例:利用Runnable接口创建线程,public class MyThread_2 implements Runnable public void run( ) ,public class MultiThread_2 public static void main(String args ) MyThread_2 thread = ne

4、w MyThread_2( ); Thread threadObj = new Thread(thread); threadObj.start(); ,10.2 线程状态的转换,一个线程的生命周期中,有四个状态,线程就是在这4个状态之间不断变换,直到死亡为止。,线程的优先级,每一个线程都有一个优先级 ,在Thread类中有3个常量MIN_PRIORITY、MAX_PRIORITY、NORMAL_PRIORITY分别表示优先级为1、10和5,它们代表最低优先级、最高优先级和普通优先级。 可以调用Thread类的setPriority(int level) 成员方法为某个线程设置优先级。 例如,假

5、设threadObj是一个Thread类对象,可以用 threadObj.setPriority(6);设置优先级。,10.3 线程控制,基本的线程控制方法 启动线程 start() 阻塞线程 wait() 唤醒线程 notify() 或 notifyAll() 或 interrupt() 线程让步 yield() 等待其它线程结束 join() 判断线程是否处于活动状态 isAlive(),10.4 多线程的同步与互斥,一般来说线程之间不是孤立的,多个线程可能共享某些资源。比如,一个线程要读取数据,另外一个线程要处理这些数据。因此,系统必须对线程进行同步控制,等到第一个线程读取完数据,第二个

6、线程才能处理该数据,从而避免错误。 简单地说,在一个时刻只能够被一个线程访问的资源称为临界资源,而访问临界资源的那段代码则被称为临界区。临界区的使用必须互斥地进行,即一个线程在临界区中执行代码时,其它线程不能够进入临界区。,synchronized,Java中,引入了对象”互斥锁”的概念,来保证共享数据操作的完整性。每个Java对象都对应于一个”互斥锁”标记,这个标记用来保证在任一时刻,只能有一个线程访问该对象。一旦某个线程获得了该锁,别的线程如果希望获得该锁,只能等待这个线程释放锁之后。 关键字synchronized用来与对象的互斥锁联系。当某个对象用synchronized 修饰时,表明

7、该对象在任一时刻只能由一个线程访问。此时,如果有第二个线程也要访问同一个对象,它也试图获取该对象的互斥锁,但因该对象已经被锁定,则第二个线程必须等待,直到锁被释放为止。,synchronized,1、synchonized方法 synchronized ,或 synchronized(this) ,synchronized,2、synchronized块 将synchronized关键字加在某代码块之前,就可声明该代码块为synchronized块,从而使该代码块成为互斥使用的代码块,即其中的代码必须获得对象的锁方能执行。,synchronized ,例:模拟银行中的存款、取款-1,publi

8、c class Account private String name; private float amount; public synchronized void deposit(float amt) float tmp = amount; tmp += amt; try Thread.sleep(1); catch (InterruptedException e) amount = tmp; public float getBalance() return amount; ,例:模拟银行中的存款、取款-2,public synchronized float withdraw(float

9、amt) float tmp = amount; if (tmp = amt) tmp -= amt; else tmp = 0; System.out.println(“预取 “ + amt + “,剩余金额不足! 仅能取走 “ + amount); try Thread.sleep(1) catch (InterruptedException e) amount = tmp; return amount; ,例:模拟银行中的存款、取款-3,public class DepositThread extends Thread /存款线程 private Account a1; private

10、float amount; public DepositThread(Account a1, float amount) this.a1 = a1; this.amount = amount; ,例:模拟银行中的存款、取款-4,public void run() synchronized (a1) float k = a1.getBalance(); try sleep(1);/模拟花费时间 catch (InterruptedException e) a1.deposit(amount); System.out.println(“现有“ + k + “,存入“ + amount + “,余额

11、“ + a1.getBalance(); ,例:模拟银行中的存款、取款-5,public class WithdrawThread extends Thread /取款线程 private Account a1; private float amount; public WithdrawThread(Account a1, float amount) this.a1 = a1; this.amount = amount; public void run() synchronized (a1) float k = a1.getBalance(); try sleep(1); /模拟花费时间 catch (InterruptedException e) float m = a1.withdraw(amount); ,例:模拟银行中的存款、取款-6,public class AccountTest public static void main(String args) Account a1 = new Account(); (new DepositThread(a1, 500).start(); (new DepositThread(a1, 200).start(); (new WithdrawThread(a1, 150).start(); ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1