第10章-线程.ppt

  1. 1、本文档共67页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第10章-线程

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 线程调度的优先级别与调度策略 public class Demo { public static void main(String[] args) { Thread t1,t2; t1 = new Thread(new createUpper()); t2 = new Thread(new createLower()); t2.setPriority(10); System.out.println( t2.getPriority() ); System.out.println( t1.getPriority() ); t2.start(); t1.start(); } } 【例16】线程的优先级 (1/2) 线程调度的优先级别与调度策略 class createUpper implements Runnable { public void run() { for(int i=65;i=90;i++) { System.out.print((char)i); } } } 【例16】线程的优先级 (2/2) class createLower implements Runnable { public void run() { for(int i=97;i=122;i++) { System.out.print((char)i); } } } 小节 一、线程概述 1.1 并行概念的引入 1.2 程序、进程与线程 1.3 线程的状态 二、创建线程 2.1 继承Thread类创建线程 2.2 实现Runnable接口创建线程 2.3 Thread类的主要方法 三、线程的同步机制 3.1 线程的异步与同步 3.2 synchronized关键字 3.3 线程间的协作 3.4 线程的挂起 四、线程调度的优先级别与调度策略 * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2.3 Thread类的主要方法 class CreateNumber implements Runnable { protected int num; private Thread t; public CreateNumber(String s) { t = new Thread(this); t.setName(s); t.start(); } 【例7】使用内嵌线程对象方式 (2/2) public void run() { Thread t = Thread.currentThread(); for(int i=1;i=3;i++) { num = (int)(Math.random()*100); System.out.println( t.getName()+ create number:+num ); try { Thread.sleep(1000); } catch(InterruptedException e){} } } } 2.3 Thread类的主要方法 public class Demo { public static void main(String[] args) { new CreateNumber(thread1); new CreateNumber(thread2); new CreateNumber(thread3); new CreateNumber(thread4); } } 【例8】使用匿名内部类 (1/2) 2.3 Thread类的主要方法 【例8】使用匿名内部类 (2/2) public void run() { Thread t=Thread.currentThread(); for(int i=1;i=3;i++) { num = (int)(Math.random()

文档评论(0)

文档精品 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6203200221000001

1亿VIP精品文档

相关文档