java 多线程基础之银行取号排队系统
2016-08-10 01:22
543 查看
1.什么是线程?多线程?
线程:可以理解为一个程序内部的顺序执行控制流。
多线程:也就是说一段代码的执行是有先后顺序的,只是看起来像同时执行的一样,假如线程a和线程b碰巧同时需要执行,那么在单核计算机的系统中的JVM虚拟机就会先判断a和b 的优先级,优先级高的先进行执行,其次是低的执行,若优先级相同则交给JVM随机挑选运行;多线程处理是cup分配的时间片决定的,时间片就是cpu给出现的线程留的可执行时间。
2.线程的创建
有两种方法:
1.子类通过继承Thread类来实现run()方法;(不够灵活)
2.子类通过继承Runnable接口来实现run()方法;(建议用这个);
3.线程的常用方法:
4.练习:银行取号排队
规则:银行有四个窗口 1个vip窗口,只能接待vip顾客,另外三个窗口优先接待vip客户
测试结果:
线程:可以理解为一个程序内部的顺序执行控制流。
多线程:也就是说一段代码的执行是有先后顺序的,只是看起来像同时执行的一样,假如线程a和线程b碰巧同时需要执行,那么在单核计算机的系统中的JVM虚拟机就会先判断a和b 的优先级,优先级高的先进行执行,其次是低的执行,若优先级相同则交给JVM随机挑选运行;多线程处理是cup分配的时间片决定的,时间片就是cpu给出现的线程留的可执行时间。
2.线程的创建
有两种方法:
1.子类通过继承Thread类来实现run()方法;(不够灵活)
2.子类通过继承Runnable接口来实现run()方法;(建议用这个);
3.线程的常用方法:
4.练习:银行取号排队
规则:银行有四个窗口 1个vip窗口,只能接待vip顾客,另外三个窗口优先接待vip客户
package 排队; public class Test01 { public static void main(String[] args){ St st = new St(); Custm[] cus = new Custm[20]; int[] ran = {1,1,1,0,1,0,1,1,1,0,1}; //int[] ran = {1,0,1,1,0,1,0,1,0,1,1}; for(int i=0;i<20;i++){ int a = (int)(Math.random()*10); cus[i] = new Custm(i+1,ran[a]); st.add(cus[i]); } Worke wort = new Worke(st, "vip", true);//vip窗口,线程1 Thread t1 = new Thread(wort); t1.start(); Worke wort1 = new Worke(st, "1号" , false);//普通窗口,线程2, Thread t2 = new Thread(wort1); t2.start(); Worke wort2 = new Worke(st, "2号" , false);//普通窗口,线程3, Thread t3 = new Thread(wort2); t3.start(); Worke wort4 = new Worke(st, "3号" , false);//普通窗口,线程4, Thread t4 = new Thread(wort4); t4.start(); } } class Custm{ int key; int num; public Custm(int n, int k){ num=n; key=k; } } class base { } class Worke implements Runnable{ boolean isvip; St s; String ThreadName; public Worke(St ss,String n ,boolean vip){ s=ss; ThreadName = n; isvip = vip; } boolean asd=true; public void run(){ while(asd){ if(s.isEmpty()==false){ if(isvip){ // synchronized(this){ Custm cu = s.dell().cus; if(cu.key==0){ System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口无人,请"+cu.num+"号【vip】顾客准备!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客就绪!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客正在办理!"); try { Thread.sleep((int)(Math.random()*500)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客已离开-------!"); // }else{ // try { // Thread.sleep((int)(Math.random()*500)); // } catch (InterruptedException e) { // TODO Auto-generated catch block // e.printStackTrace(); // } // } } }else{ // synchronized(this){ Custm cu = s.dell().cus; if(cu.key==0){ System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口无人,请"+cu.num+"号【vip】顾客准备!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客就绪!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客正在办理!"); try { Thread.sleep((int)(Math.random()*500)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【vip】顾客已离开-------!"); }else{ System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口无人,请"+cu.num+"号【普通】顾客准备!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【普通】顾客就绪!"); System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【普通】顾客正在办理!"); try { Thread.sleep((int)(Math.random()*500)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("线程id" + Thread.currentThread().getId() +"\t"+ThreadName+"---窗口"+cu.num+"号【普通】顾客已离开-------!"); } // } } }else{ asd=false; } } } } class St{ Node head; Node tail; public St(){ tail=head=null; } public boolean isEmpty(){ if(head==null){ return true; } return false; } public void add(Custm c){ Node node = new Node(c); if(isEmpty()){ head=tail=node; head.front=null; tail.next=null; }else{ tail.next=node; node.front=tail; tail=node; tail.next=null; } } public Node dell(){ if(isEmpty()==false){ if(head.next==null){ Node nod=head; head=null; return nod; }else{ Node nod=head; head=head.next; head.front=null; return nod; } }else{ System.out.println("队列空了"); return null; } } public Node del(){ if(isEmpty()==false){ Node nod = findvip(); if(nod==null){ Node temp=head; if(head.next!=null){ Node node = head.next; node.front=null; head=node; }else{ head=null; } //head.front=null; temp.next=null; return temp; }else{ if(nod==head){ nod=head; head=null; return nod; }else if(nod==tail){ Node tm = nod; nod.front.next=null; return tm; }else{ nod.front.next=nod.next; nod.next.front=nod.front; return nod; } } }else{ System.out.println("队列空了"); return null; } } public Node findvip(){ if(isEmpty()==false){ Node temp = head; while(temp!=null){ if(temp.cus.key==0){ return temp; } temp = temp.next; } } return null; } } class Node{ Custm cus; Node front; Node next; public Node(Custm c){ cus =c; front = next = null; } }
测试结果:
相关文章推荐
- java 多线程基础之银行取号排队系统
- java线程基础巩固---采用多线程方式模拟银行排队叫号以及Runnable接口存在的必要性
- Java基础__银行调度系统
- 黑马程序员-Java基础10银行调度系统
- 黑马程序员_java_基础加强_模拟银行管理系统
- 黑马程序员_java_基础加强_模拟银行管理系统
- [零基础学JAVA]Java SE实战开发-37.MIS信息管理系统实战开发[JDBC](2) 推荐
- [导入]伍迷创意随想集 之 银行排队系统的改良
- Java多线程编程基础
- Windows系统巧用Rundll32卸载Java虚拟机-Java基础-Java-编程开发
- [零基础学JAVA]Java SE实战开发-37.MIS信息管理系统实战开发[文件保存](2) 推荐
- Java多线程程序设计入门-Java基础-Java-编程开发
- 伍迷创意随想集 之 银行排队系统的改良
- java多线程基础
- [零基础学JAVA]Java SE应用部分-25.多线程(03) 推荐
- [零基础学JAVA]Java SE应用部分-26.多线程(04) 推荐
- IBM Java多线程 - 1. 线程基础
- java多线程基础
- java多线程 基础(二) Thread Runnable
- 用AJAX J2EE实现一个网上会议室系统-Java基础-Java-编程开发