java多线程(内附实例:窗口售票问题、人和叉子的问题)
java多线程的开发有两种方法:
(1)实现Runnable接口;
(2)继承Thread类;
区别:
(1)由于java中一个类只能继承一个父类,但是可以实现多个接口,所以实现Runnable接口比继承Thread更灵活。
(2)实现Runnable接口,最终还是要用Thread(Runnable)、Thread(Runnable,String)等构造函数调用,但是此时可以多个Thread共用一个Runnable,实现资源共享(详见后面售票的实例),当然也可以使用不同的Runnable(详见后面人与叉子的实例),从这点看实现Runnable接口也比继承Thread类更灵活。
联系:
Thread类其内部实现如下:
public class Thread extends Object implements Runnable,可以看出Thread类也是Runnable接口的子类;
实例1:售票问题,假设现在有三个窗口进行售票(并发执行)。
用实现Runnable接口的方法实现代码如下:
package ticket2; public class ticket2
{
public static void main(String []args)
{
Show_tickets show_ticket=new Show_tickets();
new Thread(show_ticket,"windows1").start();
new Thread(show_ticket,"windows2").start();
new Thread(show_ticket,"windows3").start();
}
}
class Show_tickets implements Runnable
{
private int tickets=10;
public void run()
{
while(true)
{
if(tickets>0)
{
System.out.println(Thread.currentThread().getName()+" showed the ticket"+tickets--);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
break;
}
}
}
}
执行结果:
windows1 showed the ticket10
windows2 showed the ticket9
windows3 showed the ticket8
windows2 showed the ticket7
windows1 showed the ticket6
windows3 showed the ticket5
windows1 showed the ticket4
windows2 showed the ticket3
windows3 showed the ticket2
windows2 showed the ticket1
用继承Thread实现如下:
package ticket1; public class ticket1
{
public static void main(String []args)
{
new Show_tickets("window1").start();
new Show_tickets("window2").start();
new Show_tickets("window3").start();
}
} class Show_tickets extends Thread
{
private int tickets=10;
private String name;
Show_tickets(String sname)
{
this.name=sname;
}
public void run()
{
while(true)
{
if(tickets>0)
{
System.out.println(name+" showed the ticket"+tickets-- );
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
break;
}
}
}
}
执行结果如下:
window1 showed the ticket10
window2 showed the ticket10
window3 showed the ticket10
window3 showed the ticket9
window2 showed the ticket9
window1 showed the ticket9
window1 showed the ticket8
window2 showed the ticket8
window3 showed the ticket8
window3 showed the ticket7
window2 showed the ticket7
window1 showed the ticket7
window1 showed the ticket6
window2 showed the ticket6
window3 showed the ticket6
window3 showed the ticket5
window2 showed the ticket5
window1 showed the ticket5
window1 showed the ticket4
window2 showed the ticket4
window3 showed the ticket4
window3 showed the ticket3
window2 showed the ticket3
window1 showed the ticket3
window1 showed the ticket2
window2 showed the ticket2
window3 showed the ticket2
window3 showed the ticket1
window2 showed the ticket1
window1 showed the ticket1
可见由于方法1中Show_tickets类是被实例化之后,给三个进程共用的,所以相当于3个窗口一共有10张票大家来卖,而方法2中由于每一个Show_tickets都被实例化为一个对象,所以其中的变量tickets也就是独立的,相当于每一个窗口都有10张票。(当然方法2中也可以用static实现共享)
实例2 人和叉子的问题,有5个人,5个叉,他们围城一圈,叉子依次摆在他们相邻的地方,只有一个人的左右手边叉子都没被用的时候,这个人才拿起叉子(左右2个都被拿起)吃饭,吃完后1秒,将叉子放下,若每个人吃一次之后就不再吃了,模仿他们吃饭的顺序。
用实现Runnable接口的方法实现代码如下:
package person1; public class personfork
{
public static void main(String []args)
{
Forks fork=new Forks();
Person person1=new Person("person1",fork);
new Thread(person1,"0").start();;
Person person2=new Person("person2",fork);
new Thread(person2,"1").start();
Person person3=new Person("person3",fork);
new Thread(person3,"2").start();
Person person4=new Person("person4",fork);
new Thread(person4,"3").start();
Person person5=new Person("person5",fork);
new Thread(person5,"4").start();
}
} class Person implements Runnable
{
private String person_name;
private Forks fork;
Person(String name,Forks fork_input)
{
this.person_name=name;
this.fork=fork_input;
}
public void run()
{
fork.get_fork();
System.out.println("i am eating " + person_name);
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
fork.put_fork();
}
}
class Forks
{
private int forks[]={0,0,0,0,0};
public synchronized void get_fork()
{
String thread_name=Thread.currentThread().getName();
int num=Integer.parseInt(thread_name);
while(1 == forks[num] || 1 == forks[(num+1)%5])
{
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
forks[num]=1;
forks[(num+1)%5]=1;
}
public synchronized void put_fork()
{
String thread_name=Thread.currentThread().getName();
int num=Integer.parseInt(thread_name);
forks[num]=0;
forks[(num+1)%5]=0;
notifyAll();
}
}
用继承Thread实现如下:
package personfork; public class personfork
{
public static void main(String []args)
{
Fork fork=new Fork();
new Person("person1","0",fork).start();
new Person("person2","1",fork).start();
new Person("person3","2",fork).start();
new Person("person4","3",fork).start();
new Person("person5","4",fork).start();
}
} class Person extends Thread
{
private String person_name;
//private String thread_name;
private Fork fork;
Person(String person_name1,String thread_name1,Fork fork1)
{
super(thread_name1);
//person_name=person_name1;
//fork=fork1;
this.person_name=person_name1;
this.fork=fork1;
}
public void run()
{
//System.out.println("I am Eating:"+person_name);
fork.get_fork();
System.out.println("I am Eating:"+person_name);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fork.put_fork();
}
}
class Fork
{
private int forks []={0,0,0,0,0};
public synchronized void get_fork()
{
int num=Integer.parseInt(Thread.currentThread().getName());
while(1==forks[num] || 1==forks[(num+1)%5])
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
forks[num]=1;
forks[(num+1)%5]=1;
}
public synchronized void put_fork()
{
int num=Integer.parseInt(Thread.currentThread().getName());
forks[num]=0;
forks[(num+1)%5]=0;
notifyAll();//唤醒其他线程
}
}
两种方法的结果一样基本都是5个人随机吃一遍,这里的Person是不同的5个人,所以在实现Runnable接口的方法中也并没有将其共享资源,而是放到5个不同的线程中。
java多线程(内附实例:窗口售票问题、人和叉子的问题)的更多相关文章
- java多线程的编程实例
java中可有两种方式实现多线程: 一种是继承Thread类: 一种是实现Runnable接口: Thread类 是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的ru ...
- java多线程找素数实例
package ltb20180106; public class FindPrime implements Runnable{ private int prime; private int q; p ...
- Java基础 继承的方式创建多线程 / 线程模拟模拟火车站开启三个窗口售票
继承的方式创建多线程 笔记: /**继承的方式创建多线程 * 线程的创建方法: * 1.创建一个继承于Thread 的子类 * 2.重写Thread类的run()方法 ,方法内实现此子线程 要完成的功 ...
- java多线程编程实例
[转]这篇文章主要介绍了java多线程编程实例,分享了几则多线程的实例代码,具有一定参考价值,加深多线程编程的理解还是很有帮助的,需要的朋友可以参考下. 1.三个售票窗口同时出售20张票程序分析: ...
- Java经典设计模式之七大结构型模式(附实例和详解)
博主在大三的时候有上过设计模式这一门课,但是当时很多都基本没有听懂,重点是也没有细听,因为觉得没什么卵用,硬是要搞那么复杂干嘛.因此设计模式建议工作半年以上的猿友阅读起来才会理解的比较深刻.当然,你没 ...
- Java设计模式之七大结构型模式(附实例和详解)
博主在大三的时候有上过设计模式这一门课,但是当时很多都基本没有听懂,重点是也没有细听,因为觉得没什么卵用,硬是要搞那么复杂干嘛.因此设计模式建议工作半年以上的猿友阅读起来才会理解的比较深刻.当然,你没 ...
- (转)Java经典设计模式(2):七大结构型模式(附实例和详解)
原文出处: 小宝鸽 总体来说设计模式分为三大类:创建型模式.结构型模式和行为型模式. 博主的上一篇文章已经提到过创建型模式,此外该文章还有设计模式概况和设计模式的六大原则.设计模式的六大原则是设计模式 ...
- Java多线程-实例解析
Java多线程实例 3种实现方法Java中的多线程有三种实现方式:1.继承Thread类,重写run方法.Thread本质上也是一个实现了Runnable的实例,他代表一个线程的实例,并且启动线程的唯 ...
- Java经典设计模式之十一种行为型模式(附实例和详解)
Java经典设计模式共有21中,分为三大类:创建型模式(5种).结构型模式(7种)和行为型模式(11种). 本文主要讲行为型模式,创建型模式和结构型模式可以看博主的另外两篇文章:Java经典设计模式之 ...
随机推荐
- PID file found but no matching process was found. Stop aborted
一般脚本部署时不会遇到这种情况,有时候自个手动处理会出现”PID file found but no matching process was found. Stop aborted”,根据意思就可以 ...
- [Docker]学习笔记--简单介绍
学习docker已经有一段时间了,一直没有静下心来好好总结一下. 最近用docker搭了一整套Gitlab的持续集成环境.(会在下一篇中详细的讲解具体步骤,敬请期待) 感觉是时候写点东西和大家一起分享 ...
- C#设计模式-单实例
单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...
- python模块学习(三)
logging模块 简单应用 import logging logging.debug('debug message') logging.info('info message') logging ...
- 我的Java开发学习之旅------>二进制、八进制、十进制、十六进制之间转换
一. 十进制与二进制之间的转换 (1) 十进制转换为二进制,分为整数部分和小数部分 ① 整数部分 方法:除2取余法,即每次将整数部分除以2,余数为该位权上的数,而商继续除以2,余数又为上一个位权 ...
- hdu 2112 HDU Today(map与dijkstra的结合使用)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 006-重装yum
报错情况: There was a problem importing one of the Python modulesrequired to run yum. The error leading ...
- Java设计模式之《单例模式》及应用场景(转发:http://www.cnblogs.com/V1haoge/p/6510196.html)
所谓单例,指的就是单实例,有且仅有一个类实例,这个单例不应该由人来控制,而应该由代码来限制,强制单例. 单例有其独有的使用场景,一般是对于那些业务逻辑上限定不能多例只能单例的情况,例如:类似于计数器之 ...
- 1.4 使用电脑测试MC20的接收英文短信功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- dom树改变监听
function unwrap(el, target) { if ( !target ) { target = el.parentNode; } while (el.firstChild) { tar ...