accp8.0转换教材第1章多线程理解与练习
一.单词部分:
①process进程 ②current当前的③thread线程④runnable可获取的
⑤interrupt中断⑥join加入⑦yield产生⑧synchronize同时发生
二.预习部分
1.线程与进程的区别:
进程是系统运行程序的基本单位
线程是进程中执行运算的最小单位
2.说明创建线程的方式有哪两种
①继承thread类
②实现Runnable接口
3.线程的生命周期可分为几个阶段,各是什么阶段
五个阶段:①创建②就绪③运行④阻塞⑤死亡
4.使用线程的什么方法可以设置线程的休眠,线程的强制执行,线程的礼让
分别为:sleep(),join(),yield()
5.什么情况下需要进行线程的同步,线程的同步有几种方式
当访问冲突时需要进行
两种方式:①同步方法②同步代码块
三.练习部分
1.使用继承Thread类的方法创建线程,显示相应内容
首先创建一个线程类:
package oneOne;
public class MyRunnableone extends Thread{
public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,来自线程"+Thread.currentThread().getName());
}
}
}
再创建main方法类去掉用就行了
package oneOne;
public class testone {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnableone my=new MyRunnableone();
MyRunnableone my1=new MyRunnableone();
my.start();
my1.start();
}
}
2.使用实现Runnable接口方式创建线程
同第一个先创建实现类:
package oneTwo;
public class MyRunnabletwo implements Runnable{
public void run() {
for(int i=1;i<=20;i++){
System.out.println(i+".你好,来自线程"+Thread.currentThread().getName());
}
}
}
再main方法:
package oneTwo;
public class testtwo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnabletwo my=new MyRunnabletwo();
MyRunnabletwo my1=new MyRunnabletwo();
Thread tr=new Thread(my);
Thread tr1=new Thread(my1);
tr.start();
tr1.start();
}
}
3.使用多线程模拟多人徒步爬山
先创建继承或者实现类(我这里用了继承):
package oneThree;
public class MyRunnablethree extends Thread{
private int time;
public int num=0;
public MyRunnablethree(String name,int time,int kio) {
super(name);
this.time=time;
this.num=kio*1000/100;
}
public void run() {
while (num>0) {
try {
Thread.sleep(this.time);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"爬完100米!");
num--;
}
System.out.println(Thread.currentThread().getName()+"到达终点!");
}
}
再main方法:
package oneThree;
public class testthree {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnablethree young=new MyRunnablethree("年轻人", 500, 1);
MyRunnablethree old=new MyRunnablethree("老年人", 1500, 1);
MyRunnablethree child=new MyRunnablethree("小孩", 600, 1);
System.out.println("**********开始爬山*********");
old.start();
young.start();
child.start();
}
}
4.显示,设置线程优先级
先继承或者实现类:
package oneFour;
public class MyRunnablefour extends Thread{
public void run() {
Thread.currentThread().setPriority(1);
System.out.println("子线程名:"+Thread.currentThread().getName()+",优先级:"+Thread.currentThread().getPriority());
}
}
再main:
package oneFour;
public class testfour {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnablefour myf=new MyRunnablefour();
myf.start();
System.out.println("*************显示默认优先级********");
System.out.println("主线程名:"+Thread.currentThread().getName()+",优先级:"+Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("***********修改默认优先级后***********");
//myf.setPriority(1);
System.out.println("主线程名:"+Thread.currentThread().getName()+",优先级:"+Thread.currentThread().getPriority());
//System.out.println("子线程名:"+MyRunnablefour.currentThread().getName()+",优先级:"+MyRunnablefour.currentThread().getPriority());
}
}
5.模拟叫号看病
先继承或实现类:
package oneFive;
public class MyRunnablefive extends Thread{
private int time;
//public int pertail=0;
public MyRunnablefive(String common,int time) {
super(common);
this.time=time;
}
public void run() {
Thread.currentThread().setPriority(8);
for(int i=1;i<=10;i++){
try {
Thread.sleep(this.time);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("特需号:"+i+"号病人在看病!");
}
}
}
再main:
package oneFive;
public class testfive {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//MyRunnablefive pertail=new MyRunnablefive("特需号", 1000);
Thread temp=new Thread(new MyRunnablefive("特需号", 400));
temp.start();
Thread.currentThread().setPriority(4);
for(int i=1;i<=50;i++){
if(i==11){
try {
temp.join();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("普通号:"+i+"号病人在看病");
}
}
}
6.多线程模拟 接力赛跑
先创建继承或者实现类:
package oneSix;
public class runSix implements Runnable{
private int meters=1000;
public runSix(){
}
@Override
public void run() {
// TODO Auto-generated method stub
//System.out.println("进来了");
while (true) {
//type type = (type) true.nextElement();
synchronized (this) {
if(meters<=100){
break;
}
System.out.println(Thread.currentThread().getName()+"拿到了接力棒!");
for (int i = 0; i < 100; i+=10) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"跑了"+(i+10)+"米!");
}
meters-=100;
}
}
}
}
再main接口类:
package oneSix;
public class testsix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
runSix ru=new runSix();
for (int i = 0; i < 5; i++) {
new Thread(ru,(i+1)+"号选手").start();
}
}
}
7.多线程模拟网络购票
桃跑跑,张票票,黄牛党,共同抢十张票,限制黄牛党只能抢一次票
先创建继承或者实现类:
package oneSeven;
public class siteSeven implements Runnable{
private int count=10;
private int num=0;
private boolean flag=false;
@Override
public void run() {
// TODO Auto-generated method stub
//System.out.println("进来了");
while (!flag) {
synchronized (this){
//System.out.println("进来了");
if(count<=0){
flag=true;
return;
}
num++;
count--;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name=Thread.currentThread().getName();
if(name.equals("黄牛党")){
System.out.println(name+"抢到第"+num+"张票,剩余"+count+"张票!");
break;
}
System.out.println(name+"抢到第"+num+"张票,剩余"+count+"张票!");
}
}
}
}
再创建main接口类:
package oneSeven;
public class testseven {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
siteSeven si=new siteSeven();
Thread per1=new Thread(si,"大东");
Thread yellow=new Thread(si,"黄牛党");
Thread per2=new Thread(si,"启圳");
per1.start();
yellow.start();
per2.start();
}
}
四:总结:
1.Thread类中的方法实现对线程对象的操作
①调整线程的优先级
②线程睡眠sleep()
③线程的强制运行join()
④线程礼让yield()
2.多线程允许程序员编写可最大利用CPU的高效程序
3.两种方式创建线程:
①声明一个继承了Thread类的子类并实现Thread类的run()方法
②声明一个实现Runnable接口的类,然后实现run()方法
原文在博客园有需要可以联系扣扣:2265682997
accp8.0转换教材第1章多线程理解与练习的更多相关文章
- accp8.0转换教材第10章Ajax和jQuery理解与练习
C/S (Client/Server)结构,即大家熟知的客户机和服务器结构. B/S(Browser/Server)结构即浏览器和服务器结构. 认识ajax .XMLHttpRequest.使用jqu ...
- accp8.0转换教材第11章JAjax加护扩展理解与练习
①杂记:前面有原生态JavaScript实现ajax这里又多了更简单的方法实现ajax ②$.get()方法的常用参数 参数 类型 说明 url String 必选,规定发送地址 data Plain ...
- accp8.0转换教材第6章连接MySQL理解与练习
JDBC_ODBC,纯java方式连接mysql 1.单词部分 ①JDBCjava连接数据库②driver manager驱动③connection连接④statement声明 ⑤execute执行⑥ ...
- accp8.0转换教材第4章MySQL高级查询(二)理解与练习
知识点:EXISTS子查询.NOT EXISTS子查询.分页查询.UNION联合查询 一.单词部分 ①exist存在②temp临时的③district区域 ④content内容⑤temporary暂时 ...
- accp8.0转换教材第9章JQuery相关知识理解与练习
自定义动画 一.单词部分: ①animate动画②remove移除③validity有效性 ④required匹配⑤pattern模式 二.预习部分 1.简述JavaScript事件和jquery事件 ...
- accp8.0转换教材第8章JavaScript对象及初识面向对象理解与练习
JavaScript数据类型,对象,构造函数,原型对象,初识原型链,对象继承 一.单词部分 ①object父类②constructor构造函数③instance实例④call调用 ⑤apply应用⑥c ...
- accp8.0转换教材第7章JavaScript操作DOM对象理解与练习
程序调试,chrome开发人员工具,DOM操作,节点和节点间的关系,节点信息,操作节点,获取元素 一.单词部分 ①alert警告②prompt提示③parentNode父节点④childNode子节点 ...
- accp8.0转换教材第5章事务、视图、索引、备份和恢复理解与练习
知识点:事务.视图.索引.数据库的备份和恢复 一.单词部分 ①transation事务②atomicity原子性③consistency一致性④isolation隔离性 ⑤durability持久性⑥ ...
- accp8.0转换教材第3章MySQL高级查询(一)理解与练习
一.单词部分 ①constraint约束②foreign外键③references参考 ④subquery子查询⑤inner内部的⑥join连接 二.预习部分 1.修改表SQL语句的关键字是什么 RE ...
随机推荐
- SQLyog-12.4.2版下载,SQLyog最新版下载,SQLyog官网下载,SQLyog Download
SQLyog-12.4.2版下载,SQLyog最新版下载,SQLyog官网下载,SQLyog Download >>>>>>>>>>> ...
- OpenCV探索之路(六):边缘检测(canny、sobel、laplacian)
边缘检测的一般步骤: 滤波--消除噪声 增强--使边界轮廓更加明显 检测--选出边缘点 Canny算法 Canny边缘检测算法被很多人推崇为当今最优秀的边缘检测算法,所以我们第一个就介绍他. open ...
- ReentrantLock深入学习
ReentrankLock 分为 非公平锁及公平锁 首先我们看一下它里面有哪些属性: private final Sync sync;Sync 这个类是 ReentrantLock的 一个静态内部类 ...
- JavaScript基础(.....持续待更)
javascript热身 一.你知道,为什么JavaScript非常值得我们学习吗? 1. 所有主流浏览器都支持JavaScript. 2. 目前,全世界大部分网页都使用JavaScript. 3. ...
- php原生自定义验证码,5分钟搞定你的问题
当然现在很多php的框架里面自带了很多很多验证码,我的这个验证码,也是当初刚刚入行的时候学习模仿的.现在照搬出来,希望对刚入门的朋友有所帮助. **************************** ...
- 细说"回车"和"换行"的故事
引言 最近在php还有c#以及memcache的shell当中经常看到\r\n的写法,刚开始还没注意, 不过后面感觉这样写有些不对头,\r表示回车 \n表示换行,那这样不是换行了两次吗? 为了解决疑 ...
- 教你一步搭建Flume分布式日志系统
在前篇几十条业务线日志系统如何收集处理?中已经介绍了Flume的众多应用场景,那此篇中先介绍如何搭建单机版日志系统. 环境 CentOS7.0 Java1.8 下载 官网下载 http://flume ...
- 分享几个python小脚本
by 梁凯 今天我想给大家分享几个python脚本,分别是: 1.公司访问外网认证脚本(最初有同事写过,我优化了一下). 2.统计周报系统所有同事的最近一篇周报. 3.统计测试技术分享里指定一个月所有 ...
- UVALive-5731
UVALive-5731 题意 一颗 n - 1 条边的有向树,要求整棵树成为强连通图,一次操作即构建一条路(一笔画), 限制: 新建的路上的所有边必须与原有的边逆向,即构建的路必须在原有的边和点上, ...
- Vue.js 运行环境搭建详解(基于windows的手把手安装教学)及vue、node基础知识普及
Vue.js 是一套构建用户界面的渐进式框架.他自身不是一个全能框架——只聚焦于视图层.因此它非常容易学习,非常容易与其它库或已有项目整合.在与相关工具和支持库一起使用时,Vue.js 也能完美地驱动 ...