黑马程序员--java多线程模拟实现多窗口售票大厅工作

本文主要是介绍黑马程序员--java多线程模拟实现多窗口售票大厅工作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------

1.创建线程的两种常用方法

创建线程的第二种方法:

1,定义类实现Runnable接口

2.覆盖Runnable接口中的run方法

3.通过Thread类建立线程对象

4.Runnable接口的子类对象作为实际参数传递给Thread类的构造器

5.调用Thread;类的start方法开启线程并调用Runnable接口子类的run方法.

实现方式和继承方式的区别?

实现方式的好处是避免了单继承的局限性,在定义线程时建议使用实现方式

两种方法区别:

继承Thread:线程代码存放在Thread子类的run方法中

实现Runnable接口,代码存放在接口的run方法中

第一种方法代码:

class ThreadDemo1//演示创建线程的方法一继承Thread类 

{

public static void main(String[] args) 

{

MyThread1 m1=new MyThread1();

MyThread1 m2=new MyThread1();

MyThread1 m3=new MyThread1();

m1.start();

m2.start();

m3.start();

}

}

class MyThread1 extends Thread

{

private static int num=100;

public void run()

{

while(num>0)

{

System.out.println("当期线程: "+Thread.currentThread().getName()+"num: "+num--);

}

}

}

第二种方法:代码

class ThreadDemo2//创建线程的方法二 实现Runnable接口

{

public static void main(String[] args) 

{

MyThread mt1=new MyThread();

MyThread mt2=new MyThread();

MyThread mt3=new MyThread();

Thread th1=new Thread(mt1);

Thread th2=new Thread(mt2);

Thread th3=new Thread(mt3);

th1.start();

th2.start();

th3.start();

}

}

class MyThread implements Runnable

{

private static int num=100;

public void run()//覆写run()方法

{

while(num>0)

{

System.out.println(Thread.currentThread().getName()+" num="+num--);

}

}

}

2.多线程基本知识

同步的前提:

1.必须要有两个或者两个以上的线程

2.必须是多个线程使用同一个锁

同步函数的锁是this

静态函数的锁是Class对象

为什么这些操作线程的方法,wait(),notify(),notifyAll()要定义在Object类中.?因为这些方法在操作同步中线程时,都必须要标识他们所操作线程只有的锁,只有同一个锁上的被等待线程,可以被同一个锁上notify唤醒.不可以对不同锁中的线程进行唤醒.

也就是说,等待和唤醒必须是同一个锁,而锁可以是任意对象,所以可以被任意对象调用的方法定义在Object类中.

JDK1.5 中提供了多线程升级解决方案.

将同步synchronized替换成现实lock操作.

object中的wait,notify notifyAll,替换了condition对象

该对象可以lock锁 进行获取.

实现本方唤醒对方的机制.

线程停止的方法:

之前用stop方法,后来就不再使用了,

目前只有一个方法就是run()方法的结束.

3.一个死锁的例子

class DeadLockDemo//演示死锁 

{

public static void main(String[] args) 

{

DeadLock d=new DeadLock();

Thread t1=new Thread(d);

Thread t2=new Thread(d);

t1.start();

t2.start();

}

}

class DeadLock implements Runnable

{

Object lock1=new Object();

Object lock2=new Object();

boolean flag=true;

public void run()

{

if(flag)

{

flag=false;

synchronized(lock1)

{

System.out.println("lock1");

synchronized(lock2)

{

System.out.println("if lock2");

}

}

}

else

{

flag=true;

synchronized(lock2)

{

System.out.println("else lock2");

synchronized(lock1)

{

System.out.println("else lock1");

}

}

}

}

}

4.线程间通信

线程间通信其实就是多线程操作同一个资源

waitsleep的区别:

sleep只释放资源不释放锁

wait即释放资源又释放锁

生产与消费的一个代码例子:

class  InputOutputDemo//进程间通信演示代码

{

public static void main(String[] args) 

{

Res s=new Res();

Input in=new Input(s);

Output out=new Output(s);

Thread t1=new Thread(in);

Thread t2=new Thread(out);

t1.start();

t2.start();

}

}

class Res//资源类

{

public String name;

public String sex;

}

class Input implements Runnable//生产者

{

private Res r;

public Input(Res r)

{

this.r=r;

}

public void run()

{

boolean flag=true;

while(true)

{

synchronized("haha")

{

if(flag)

{

flag=false;

r.name="mike";

r.sex="man";

}else

{

flag=true;

r.name="丽丽";

r.sex="";

}

}

}

}

}

class Output implements Runnable//消费者

{

private Res r;

public Output(Res r)

{

this.r=r;

}

public void run()

{

boolean flag=true;

while(true)

{

synchronized("haha")

{

if(flag)

{

flag=false;

System.out.println(r.name+": "+r.sex);

}else

{

flag=true;

System.out.println(r.name+": "+r.sex);

}

}

}

}

}

等待唤醒机制对上面的生产者与消费者就行优化:

join方法的特点:

A线程执行到了B线程的join方法时,A就会等待,B线程都执行完,A才会执行.join可以用来临时加入线程.


4.1线程间通信JDK1.5后新方法

import java.util.concurrent.locks.*;

class ProConDemo2//演示生产者消费者

{

public static void main(String[] args)

{

Resource2 r=new Resource2();

Producer2 pro=new Producer2(r);

Consumer2 con=new Consumer2(r);

Thread t1=new Thread(pro);

Thread t2=new Thread(con);

Thread t3=new Thread(pro);

Thread t4=new Thread(con);

t1.start();

t2.start();

t3.start();

t4.start();

}

}

class Resource2//d

{

private String name;

private int count=1;

private boolean flag=false;

private Lock lock=new ReentrantLock();//since JDK1.5

private Condition condition_pro=lock.newCondition();

private Condition condition_con=lock.newCondition();

public  void set(String name) throws Exception

{

lock.lock();

try

{

while(flag)

condition_pro.await();

this.name=name+(count++);

System.out.println("生产了:"+this.name);

flag=true;

condition_con.signal();

}

finally

{

 lock.unlock();///

}

}

public  void out()throws Exception

{

lock.lock();

try

{

while(!flag)

condition_con.await();

System.out.println("消费了.............."+this.name);

flag=false;

condition_pro.signal();

}

finally{lock.unlock();}   ///

}

}

class Producer2 implements Runnable

{

private Resource2 r;

public Producer2(Resource2 r)

{

this.r=r;

}

public void run()

{

while(true)

{

try

{

r.set("商品");

}

catch (Exception e){}

}

}

}

class Consumer2 implements Runnable

{

private Resource2 r;

public Consumer2(Resource2 r)

{

this.r=r;

}

public void run()

{

while(true)

{

try

{

r.out();

}

catch (Exception e){}

}

}

}

5.图形化界面实现多窗口售票功能

 说明:利用多线程做的模拟售票系统.为了真实的演示,为程序增加了图形化界面.界面如下图所示.有四个子线程分别代表四个窗口进行售票.四个售票窗口共同拥有一个票库.

图片

import java.awt.*;

import java.awt.event.*;

class SaleTicketDemo//演示可视化界面售票类 

{

public static void main(String[] args) 

{

new SaleTicket(1000).start();

}

}

class SaleTicket //售票类

{

private  Frame frame=new Frame("售票大厅");

private TextArea window1=new TextArea("1号售票窗口",10,40,TextArea.SCROLLBARS_VERTICAL_ONLY);//定义售票窗口

private TextArea window2=new TextArea("2号售票窗口",10,40,TextArea.SCROLLBARS_VERTICAL_ONLY);//定义售票窗口

private TextArea window3=new TextArea("3号售票窗口",10,40,TextArea.SCROLLBARS_VERTICAL_ONLY);//定义售票窗口

private TextArea window4=new TextArea("4号售票窗口",10,40,TextArea.SCROLLBARS_VERTICAL_ONLY);//定义一个公布余票信息窗口

private Panel panel1=new Panel();

private Panel panel2=new Panel();

private final TextField field=new TextField(60);//定义一个显示板,用于显示余票等信息.

private Button but1=new Button("开始售票");//定义一个按钮,用于启动售票系统

private Button but2=new Button("重新设置");//清空数据,重新开始演示

private  int num=1000;//定义初始化票总数

private int num2=1000;//定义该变量用于记录num,用于重新设置时.

public SaleTicket(){}//无参构造器

public SaleTicket(int num)//含参数构造器,参数为设置票总数

{

num2=num;

setNum(num);

}

public void setNum(int num)

{

if(num<0||num>10000)

{

System.out.println("您设置的票数非法.");

}else

{

this.num=num;

num2=num;

}

}

public int getNum()

{

return num;

}

public int saleNum()//每调用一次,票数少一张,并返回当前票数

{

return num--;

}

private void init()//初始化图形界面函数

{

frame.setBounds(50,50,610,410);//设置大小和位置

frame.setBackground(new Color(255,218,185));

frame.setLayout(new FlowLayout());

but1.setForeground(new Color(148, 0, 211));

window1.setForeground(new Color(255,7,211));

window2.setForeground(new Color(255,7,211));

window3.setForeground(new Color(255,7,211));

window4.setForeground(new Color(255,7,211));

field.setForeground(new Color(0,0,255));

flushNum();

frame.add(panel1);

frame.add(panel2);

panel2.setLayout(new GridLayout(2,2));

panel1.add(but2);

panel1.add(field);

panel1.add(but1);

panel2.add(window1);

panel2.add(window2);

panel2.add(window3);

panel2.add(window4);

//为窗口增加关闭监听器

frame.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

frame.setVisible(true);

but1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

flushNum();

System.out.println(e.toString());

saleTicket();

}

});

but2.setForeground(new Color(255,0,0));

but2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

window1.setText("");

window2.setText("");

window3.setText("");

window4.setText("");

setNum(num2);

flushNum();

System.out.println(e.toString());

}

});

}

public void start()//开始运营系统,是该类的如楼函数

{

init();

}

class Window1 implements Runnable//窗口1,用于出售票方法

{

int temp=1;

public void run()

{

window1.setForeground(new Color(255,0,0));

while(getNum()>0)

{

synchronized(SaleTicket.class)

{

window1.append("1#窗口售出票号:"+(saleNum()+1)+"\r\n"+"累计售出: "+(temp++)+"\r\n");

}

flushNum();

}

window1.append("对不起,票已经售完,该窗口今天累计售出:"+(temp-1)+"张票");

}

}

class Window2 implements Runnable//窗口2

{

int temp=1;

public void run()

{

window2.setForeground(new Color(120,5,250));

while(getNum()>0)

{

synchronized(SaleTicket.class)

{

window2.append("2#窗口售出票号:"+(saleNum()+1)+"\r\n"+"累计售出: "+(temp++)+"\r\n");

}

flushNum();

}

window2.append("对不起,票已经售完,该窗口今天累计售出:"+(temp-1)+"张票");

}

}

class Window3 implements Runnable//窗口2

{

int temp=1;

public  void run()

{

while(getNum()>0)

{

synchronized(SaleTicket.class)

{

window3.append("3#窗口售出票号:"+(saleNum()+1)+"\r\n"+"累计售出: "+(temp++)+"\r\n");

}

flushNum();

}

window3.append("对不起,票已经售完,该窗口今天累计售出:"+(temp-1)+"张票");

}

}

class Window4 implements Runnable//窗口2

{

int temp=1;

public void run()

{

window4.setForeground(new Color(0,0,255));

while(getNum()>0)

{   

synchronized(SaleTicket.class)

{

window4.append("4#窗口售出票号:"+(saleNum()+1)+"\r\n"+"累计售出: "+(temp++)+"\r\n");

}

flushNum();

}

window4.append("对不起,票已经售完,该窗口今天累计售出:"+(temp-1)+"张票");

}

}

private void saleTicket()//开始售票

{

Window1 w1=new Window1();

Window2 w2=new Window2();

Window3 w3=new Window3();

Window4 w4=new Window4();

Thread t1=new Thread(w1);

Thread t2=new Thread(w2);

Thread t3=new Thread(w3);

Thread t4=new Thread(w4);

t1.start();

t2.start();

t3.start();

t4.start();

}

private void flushNum()//刷新剩余票数

{

if(getNum()<=0)

field.setText("对不起,北京--上海的车票已经售完.");

else

field.setText("还有票数 "+getNum()+"张 北京--上海");

}

}

下图是该程序运行时状态.

图片

下图为运行结束即票资源售完后截图.

图片

这篇关于黑马程序员--java多线程模拟实现多窗口售票大厅工作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/380524

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与