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;
}
}
測試結果: