????Java??????÷?????
???????????? ???????[ 2015/3/18 14:05:59 ] ????????Java ??????? ?????
????????java??????÷?????????????Java???????????????????о???????????????????????????Java??????????????÷?????
????1.???????
??????Java?д????????????????????Thread??????Runnable??????????Runnable??????????????Thread??????????????????Thread????Runnable??????????????????Thread?????????????????Thread??????????
????public Thread();
????public Thread(Runnable target);
????public Thread(String name);
????public Thread(Runnable target??String name);
????public Thread(ThreadGroup group??Runnable target);
????public Thread(ThreadGroup group??String name);
????public Thread(ThreadGroup group??Runnable target??String name);
????public Thread(ThreadGroup group??Runnable target??String name??long stackSize);
??????????????Thread????run????
????public class ThreadDemo1{
????public static void main(String[]args){
????Demo d=new Demo();
????d.start();
????for(int i=0;i<60;i++){
????System.out.println(Thread.currentThread().getName()+i);
????}
????}
????}
????class Demo extends Thread{
????public void run(){
????for(int i=0;i<60;i++){
????System.out.println(Thread.currentThread().getName()+i);
????}
????}
????}
????????????
????public class ThreadDemo2{
????public static void main(String[]args){
????Demo2 d=new Demo2();
????Thread t=new Thread(d);
????t.start();
????for(int x=0;x<60;x++){
????System.out.println(Thread.currentThread().getName()+x);
????}
????}
????}
????class Demo2 implements Runnable{
????public void run(){
????for(int x=0;x<60;x++){
????System.out.println(Thread.currentThread().getName()+x);
????}
????}
????}
????2.????????????
?????????????????????????????????????????????????С?????????????????????????????????????Thread???е???????п?????????????Thread???к??????????????????
????//??????
????publicvoid start();
????publicvoid run();
????//???????????
????publicvoid resume();//?????????
????publicvoid suspend();//?????????
????publicstaticvoid sleep(long millis);
????publicstaticvoid sleep(long millis??int nanos);
????//??????
????publicvoid stop();//?????????
????publicvoid interrupt();
????//????????
????publicboolean isAlive();
????publicboolean isInterrupted();
????publicstaticboolean interrupted();
????//join????
????publicvoid join()throws InterruptedException;
???????????????????????run?????е?????????????????????????????????????Thread??????????????????????????????????????setPriority?????????(setName)???????????setDaemon?????
??????????start??????????????run?????е?????????????????????????Thread???isAlive???????ж??????????????????????????????????isAlive????true????isAlive????false??????????????????????????????????????????????????????????к??????????????л???????????????isAlive???????
???????????????run???????????????run???????????????????????????????е?????У???????????????????????????С?????????????suspend??sleep???????suspend???????????????resume?????????????????sleep???????????????趨????????????????????????????????????????????????У?????????????????????????е??????
?????????sleep?????????????????
????1.sleep?????????????????????????????????????????????????????????????(1??000??000???????1????)????????????????????Java????????????????????????????sleep??????????Java????????????????????
????2.?????sleep????????????throws??try{…}catch{…}?????run??????????throws????????????try{…}catch{…}??????????????????У????interrupt?????ж?????sleep????????InterruptedException????sleep????????????£?
????publicstaticvoid sleep(long millis)throws InterruptedException
????publicstaticvoid sleep(long millis??int nanos)throws InterruptedException
?????????????????????????
????1.????????????????????????????run????????????????
????2.???stop?????????????????????????????????stop??suspend??resume???????????????????????????
????3.???interrupt?????ж?????
????1.???????????????
??????run????????????????????????run????????????????????????????????????????м???????????????????????????????????????????????£?????????Щ??????????????У???while??????????????????????????????????while(true){…}?????????????while???????????????????????????????????boolean?????????????????????????true??false??????while?????????????????????????????????????????????
????join????????????????е???????????С?????????????????????start????????????????????????????????start?????????????????????????????????????????join??????????????join????????????????е?start???????????????????????????????????????join????????????????????????????????С??????????????join???÷???
????3.???????????
???????????????????????????????????????????????????????????????????????????????????????????У????1???????????
?????????????????????????????????????????????????????й????У???????????С?
???????????飺
????public class ThreadDemo3{
????public static void main(String[]args){
????Ticket t=new Ticket();
????Thread t1=new Thread(t??"?????");
????Thread t2=new Thread(t??"?????");
????Thread t3=new Thread(t??"??????");
????Thread t4=new Thread(t??"??????");
????t1.start();
????t2.start();
????t3.start();
????t4.start();
????}
????}
????class Ticket implements Runnable{
????private int ticket=400;
????public void run(){
????while(true){
????synchronized(new Object()){
????try{
????Thread.sleep(1);
????}catch(InterruptedException e){
????//TODO Auto-generated catch block
????e.printStackTrace();
????}
????if(ticket<=0)
????break;
????System.out.println(Thread.currentThread().getName()+"---???"+ticket--);
????}
????}
????}
????}
???????????
????public class ThreadDemo3{
????public static void main(String[]args){
????Ticket t=new Ticket();
????Thread t1=new Thread(t??"?????");
????Thread t2=new Thread(t??"?????");
????Thread t3=new Thread(t??"??????");
????Thread t4=new Thread(t??"??????");
????t1.start();
????t2.start();
????t3.start();
????t4.start();
????}
????}
????class Ticket implements Runnable{
????private int ticket=4000;
????public synchronized void saleTicket(){
????if(ticket>0)
????System.out.println(Thread.currentThread().getName()+"?????"+ticket--);
????}
????public void run(){
????while(true){
????saleTicket();
????}
????}
????}
???????????????this??????????????class
????????????
????public class ThreadDemo3{
????public static void main(String[]args){
????class Person{
????public String name;
????private String gender;
????public void set(String name??String gender){
????this.name=name;
????this.gender=gender;
????}
????public void get(){
????System.out.println(this.name+"...."+this.gender);
????}
????}
????final Person p=new Person();
????new Thread(new Runnable(){
????public void run(){
????int x=0;
????while(true){
????if(x==0){
????p.set("????"??"??");
????}else{
????p.set("lili"??"nv");
????}
????x=(x+1)%2;
????}
????}
????}).start();
????new Thread(new Runnable(){
????public void run(){
????while(true){
????p.get();
????}
????}
????}).start();
????}
????}
????/*
????????....??
????????....??
????lili....nv
????lili....??
????????....nv
????lili....??
????*/
??????????????
????public class ThreadDemo3{
????public static void main(String[]args){
????class Person{
????public String name;
????private String gender;
????public void set(String name??String gender){
????this.name=name;
????this.gender=gender;
????}
????public void get(){
????System.out.println(this.name+"...."+this.gender);
????}
????}
????final Person p=new Person();
????new Thread(new Runnable(){
????public void run(){
????int x=0;
????while(true){
????synchronized(p){
????if(x==0){
????p.set("????"??"??");
????}else{
????p.set("lili"??"nv");
????}
????x=(x+1)%2;
????}
????}
????}
????}).start();
????new Thread(new Runnable(){
????public void run(){
????while(true){
????synchronized(p){
????p.get();
????}
????}
????}
????}).start();
????}
????}
????/*
????lili....nv
????lili....nv
????lili....nv
????lili....nv
????lili....nv
????lili....nv
????????....??
????????....??
????????....??
????????....??
????*/
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11