Java ???thread (???)???
???????????? ???????[ 2014/2/19 9:20:48 ] ??????????? Java ????
??????????????У? g() ??????????f() ??????
????g()????????????"g() is done!" ?? ???????f() ????????????. ????g() ????f()?? ???????????к???????!
?????????? ???g() ?? f() ????????????????? ????????????????е????????(?????)?????.
???????????????? ???????????????д???????????У? ??????δ???????? ????????????????????????.
??????. 1???????java??????????.
????????????:
package Thread_kng;
class M_thrd_1 extends Thread{ //extends
public M_thrd_1(String name){
super(name);
}
public void run(){ //overwrite the method run() of superclass
while (true){
System.out.printf("Thread " + this.getName()+ " is runing!
");
}
//System.out.printf("f() is done!
"); //compilation fail
}
}
public class M_thread_expl{
public static void g(){
M_thrd_1 s = new M_thrd_1("T1");
s.start(); //start()method is extended from superclass?? it will call the method run()
M_thrd_1 s2 = new M_thrd_1("T2");
s2.start();
System.out.printf("g() is done!
");
}
?????????????У? ?? M_thrd_1??????????Thread. ????д??run????.
???????????g()??????.
?????????????????M_thrd_1?????s?? s1 ?????.
?????????start()????.
????start()?????????Thread?? ???????1?????? ??????run()????.
??????g()???汾??????????????"THread Main is running"
???????????????:
???????? ?????? ????????T1??T2 ??Main ??????????????.
?????????? ?????????3?????.
????????1?????????.
?????????main????????????M_thrd_1????? ??????start()???????????????????.
????????1?????????????T1
??????1?????????????T2
?????????????????????????????????? ?????????????????????????????Main!
?????????????????????3????????????????????!
??????. Java ???????????????????.
????java????????????? ????????. ??????????java????????Thread.
????6.1 ???1. ????Thread????????
????????????????????:
???????崠??????:
????6.1.1 ??????: ???1???? ????????Thread. ????????д????д???run() ??????.
??????????????????е?M_thrd_1??
class M_thrd_1 extends Thread{ //extends
public M_thrd_1(String name){
super(name);
}
public void run(){ //overwrite the method run() of superclass
while (true){
System.out.printf("Thread " + this.getName()+ " is runing!
");
}
//System.out.printf("f() is done!
"); //compilation fail
}
}
????????????? ???????????е???????д????run() ????.
????6.1.2 ??????: ???1???????????? ????start()????
????????
????M_thrd_1 s = new M_thrd();
????s. start();
????????start() ????M_thrd_1 ????????Thread??.
???????????JDK API ??start() ?????????:
????void start()
??????????????У?Java ?????????????? run ??????
????????????? ??????start() ?????? ?????и????? ???????????????????????.
?????????????????????? ?????run()????????.
?????????????????????????????д??run()????ok??. ?????дstart()????.
??????? ?????????s.run()?? ???????????????run???????? ????п???????????. ???????.
????6.2 ???2. ????1???? ???Runnable ???
?????????????????????
????6.2.1 ??????: ???1???? ?????Runnable. ????????д????д???run() ??????.
????????:
class M_thrd_2 implements Runnable{ //extends
public int id = 0;
public void run(){ //overwrite the method run() of superclass
while (true){
System.out.printf("%s: Thread " + this.id+ " is runing!
"?? Thread.currentThread().getName());
}
//System.out.printf("f() is done!
"); //compilation fail
}
}
????????????? M_thrd_2 ???????Runnable
????????д????Runnable ???????run();
???????. currentThread() ????Thread?????????????? ??????????????????????.
??????
???·???
??????????????????
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