????Java?????????Thread?????????????????????????????????????????????????????????????Thread????????????????????Thread??????????????????????????????????????????????
???????????Java.lang.Thread??????????μ??????????????run()??????
?????????Runnalbe????????Runnalbe????е?run()??????
???????Java???????????????????????????????Щ??????????????????????????
??????Java?У??????????У?????????????????μ??????????????????????.??????????????????????????????????? Thread?????????????????????????????????????????????????????????????????????????????????????????????????????Runnable?????????????????????????????????Java???????????????????
????????????????????????Runnable??????????????????????????????????????????.
??????1????????Thread?????????????
?????????????????????????????????????????????????????????????????100?????????????????????????????????????????????????????????????????????????????????CPU????????????????????????????????????????δ??????????????
public class MutliThreadDemo {
public static void main(String [] args){
MutliThread m1=new MutliThread("Window 1");
MutliThread m2=new MutliThread("Window 2");
MutliThread m3=new MutliThread("Window 3");
m1.start();
m2.start();
m3.start();
}
}
class MutliThread extends Thread{
private int ticket=100;//??????????100???
MutliThread(String name){
super(name);//???????????????????
}
public void run(){
while(ticket>0){
System.out.println(ticket--+" is saled by "+Thread.currentThread().getName());
}
}
}
?????????ж??????????????????Thread??????????????????MutliThreadDemo??????????д????????????????????start()????????????????
???????????????????????????100?????????????κι???????????????????????????????????????????л?????CPU????????????????????????????????ν?????У????????????????????е?????£??е????????????????????????????????е???????????????????????????Щ????
????????? ???????Thread?????????????????е?????????????????????????????????????????????????? ??
??????2????????Runnable??????????????
public class MutliThreadDemo2 {
public static void main(String [] args){
MutliThread m1=new MutliThread("Window 1");
MutliThread m2=new MutliThread("Window 2");
MutliThread m3=new MutliThread("Window 3");
Thread t1=new Thread(m1);
Thread t2=new Thread(m2);
Thread t3=new Thread(m3);
t1.start();
t2.start();
t3.start();
}
}
class MutliThread implements Runnable{
private int ticket=100;//??????????100???
private String name;
MutliThread(String name){
this.name=name;
}
public void run(){
while(ticket>0){
System.out.println(ticket--+" is saled by "+name);
}
}
}
??????????????????????????????????????????????100???????????????????????1????????С??????????????????100??????е????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????100????????????????????????4.2.1???????С??????????????????100??????е????????????????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????