您的位置:首页 > 编程语言 > Java开发

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客户

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 线程 进程