Java?? Runnable? Thread? ???? ??????
May 07, 2023 pm 05:19 PMJava?? ?????? ???? ???? ? ??? ????. ??? Thread ???? ???? ???, ?? ??? Runnable ?????? ???? ????. Thread ???? java.lang ???? ???? ????. ???? Thread ???? ???? ? ???? run() ???? ????? ? ?? ??? ??? ??? ? ????. ??? ???? ??? ?? ???? ??? ? ??? ?? ? ???? ?? ?????.
?? ?? ???:
package?org.thread.demo;??class?MyThread?extends?Thread{??private?String?name;??public?MyThread(String?name)?{??super();??this.name?=?name;??}??public?void?run(){??for(int?i=0;i<10;i++){ System.out.println("線程開(kāi)始:"+this.name+",i="+i); } } } package org.thread.demo; public class ThreadDemo01 { public static void main(String[] args) { MyThread mt1=new MyThread("線程a"); MyThread mt2=new MyThread("線程b"); mt1.run(); mt2.run(); } }
??? ?? ??? ?? ??????. ?? ? ?? ??? ??? ?? ? ?? ??? ???? ?? ???? ????. start() ???? ???? JVM? ?? run() ???? ????? ?? JDK ???? ?? ? ????. ???? ????? ?? start() ???? ?????.
package org.thread.demo; public class ThreadDemo01 { public static void main(String[] args) { MyThread mt1=new MyThread("線程a"); MyThread mt2=new MyThread("線程b"); mt1.start(); mt2.start(); } };
??? ???? ????? ??? ??? ????? ??? ? ????. ???? ? ?? ???? ???? ?? start() ???? ???? ????
JDK ?? ???? src.zip? ?? Java ?? ???????. ? ??? ?? Thread?? start() ???? ??? ??? ? ????? private Native void start0()? ???? ?? ??? ? ????. ???? ?? ?? ??? ?? ??? ??? ? ??, ?? ??? JNI ??(java Native Interface)? ??? ?????.
Runnable ?????
?? ???? ?? ??? ????? Thread ?????? Runnable ????? Finish? ?????.
public interface Runnable{ public void run(); }
?:
package org.runnable.demo; class MyThread implements Runnable{ private String name; public MyThread(String name) { this.name = name; }public void run(){ for(int i=0;i<100;i++){ System.out.println("線程開(kāi)始:"+this.name+",i="+i); } } };
??? Runnable? ???? ??? ?? ????? start() ???? ?? Thread ????? ????. ?? Thread ???? ???? public Thread(Runnable targer)?? ???? ???, ? ???? Runnable? ?? ??? ????? ??????, ?? Runnable? ???? ?????? Thread ???? ?? ??? ? ??? ???. (start()? ??? ???? ??? ? ????):
package org.runnable.demo; import org.runnable.demo.MyThread; public class ThreadDemo01 { public static void main(String[] args) { MyThread mt1=new MyThread("線程a"); MyThread mt2=new MyThread("線程b"); new Thread(mt1).start(); new Thread(mt2).start(); } }
? ?? ?? ??? ???? ??:
???? ???? ?? ???? ? Runnable ?????? ??? ?? ?? ?????. Runnable ????? ??? ??? ? Thread ???? ???? ??? ?? ??? ?? ?????.
??? ??? ??? ????. ???? ?? ?????? ??? ? ????.
?? ??? ??
Thread ???? ?? ???? ?? ?? ????? ?? ?? ?????.
package org.demo.dff; class MyThread extends Thread{ private int ticket=10; public void run(){ for(int i=0;i<20;i++){ if(this.ticket>0){??System.out.println("賣票:ticket"+this.ticket--);??}??}??}??};
??? ? ?? ??? ??? ?? ??? ??? ???? ? ?????.
package?org.demo.dff;??public?class?ThreadTicket?{??public?static?void?main(String[]?args)?{??MyThread?mt1=new?MyThread();??MyThread?mt2=new?MyThread();??MyThread?mt3=new?MyThread();??mt1.start();//每個(gè)線程都各賣了10張,共賣了30張票??mt2.start();//但實(shí)際只有10張票,每個(gè)線程都賣自己的票??mt3.start();//沒(méi)有達(dá)到資源共享??}??}
Runnable? ???? ??? ??? ?????. ?? ?? ???.
package?org.demo.runnable;??class?MyThread?implements?Runnable{??private?int?ticket=10;??public?void?run(){??for(int?i=0;i<20;i++){ if(this.ticket>0){??System.out.println("賣票:ticket"+this.ticket--);??}??}??}??}??package?org.demo.runnable;??public?class?RunnableTicket?{??public?static?void?main(String[]?args)?{??MyThread?mt=new?MyThread();??new?Thread(mt).start();//同一個(gè)mt,但是在Thread中就不可以,如果用同一??new?Thread(mt).start();//個(gè)實(shí)例化對(duì)象mt,就會(huì)出現(xiàn)異常??new?Thread(mt).start();??}??};
?? ?????? 3?? ???? ??? ? 10?? ??? ???????. ?? Runnable? ???? ?????? ????? ?????. ?? ??? ??? ? ????.
Runnable ?????? Thread ?? ??:
public ??? Thread? Runnable? ???? ??? ?????.
Thread ???? Runnable ?????? ?? ???? ??? ??????.
? ??? Java?? Runnable? Thread? ???? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











Java????? ?? ??? ? ?? ???? ??? ????. 1. Entryset? ???? ?? ?? ??? ?????. ?? ???? ????? ?????. 2. ?? ?? ?? ???? ?? ? ?? ?? ?? ?????. 3. ?? ??? ?????? Java8? foreach? ??????. Entryset? ?? ? ? ?? ?? ? ?? ??? ???? ? ??? ?? ?????. ? ??? ?? ?? ?? ?????? ?????. ?? ? ? ??? ?? ?? Keyset () ?? values ()? ????? ?? ?? ?? Map.Get (?)? ?? ?? ?? ? ????. Java 8? foreach ((?, ?)-& gt? ??? ? ????

?? ??? ??? ???? ???? ? ??? ?? ?? ???? ?? ? ????. 1. ??. ofnullable? null ??? ??? ???? ?????. ?? ??, ??? ?? ??? ? Orelse? ???? ???? ? ???? ??? ???? ?????. 2. ?? ?? ?? ???? ?? ?? ???? NPE? ???? ??? ??? ??? ???? ???? ???? ?????. 3. ??? ??? ???? ??? ? ???, ??? ???? ???? ?? ??? ?? ?????. ??? ??? ??? ???? ??? ??? Orelse? ?? ?????. 4. ?? ???? ??? ??? ?? ??? ??? ???? ???? ?? ???? ???? ???? ?? ??? ?? ????? NU? ?? ?????.

Java??? ?? ???? ?? ?? ??? ????? ???? ? ???? ???? ?? ?? ??? ??? ???? ? ?????. 1. ??? ??? ?? ? ????????. ?? () ???? ?? ???? ?? ??? ?????. ??? ?? ??? ?? ???? ?? ????? ???? ?? ????? ???? ?????. 2. ???? ?? ?? ? ?? ???????, ??? ???? ?? ?? ??? ??? ??? ??? Compare () ???? ?? ????, ??? ?? ??? ??? ? ??? ?? ??? ?? ?????. ?? ???? ??? ??? ?? ?? ? ?? ? ? ??? ??? ??? ????? ??? ??? ??? ????.

java.io.notserializableException? ????? ?? ?? ??? ??? ???? ?? ???? ??? ??? ?????? ???? ?? ? ??? ??? ??? ????? ???? ????. 1. ?? ???? ??? ??????. 2. ???? ?? ??? ?? ???? ??? ??? ?????????. 3. ??? ? ????? ?? ??? ??? ??????. 4. ?? ?? ?? ? ???? ? ??? ??? ??????. 5. ?????? ???? ?? ???? ??????. 6. ? ??? ?? ?? ??? ??? ?? ?? ??? ?? ??? ??? ???? ?? ??? ??????. 7. ??? ??????

Java? ?? ??? ??? ????? ?? ? ???? ???? ???? ???? ???? ????. 1. ???? ?? ?? ??? ?? ???? ???? InputStreamReader ? OutputStreamWriter? ???? ??? ?? ???? ???? ??? ?? ? ?? ??? ??????. 2. ???? ???? ???? ?? ? ? ?? ?? ??????? ??? ??? ?? ??? ???? ?????? ???? ?? ??? ??????. 3. String.getBytes () ? Newstring (byte [])????? ???? ??? ??? ?? ??? ??? ??? ?? ?? Standardcharsets.utf_8? ???? ??????. ???,

JavaScript ??? ??? ?? ?? ? ?? ???? ????. ?? ???? ???, ??, ??, ?, ???? ?? ? ??? ?????. ?? ????? ?? ?? ? ? ??? ????? ?? ??? ??? ????. ??, ?? ? ??? ?? ?? ??? ??? ??? ???? ??? ??? ???? ??? ?? ??? ????. ?? ? ????? ??? ???? ? ??? ? ??? TypeofNull? ??? ?????? ??? ? ????. ? ? ?? ??? ???? ?????? ????? ???? ??? ???? ? ??? ? ? ????.

??? ??? Java?? Lambda ??? ??? ????? ????? ???? ????. ??? ??? ??? ??? ?????? ????? ??? Java 8?? ?? ? ?? ???? ?? ?????. ??? ?? ???? ?? ?????? ???? ?? ???? ????. ?? ??, System.out :: println? s-> system.out.println? ?????. ??? ???? ? ?? ?? ??? ????. 1. ?? ??? ?? (className :: staticMethodName); 2. ???? ??? ?? (?? ??? ???, ???? :: methodName); 3.

injava, thestatickeywordmeansamembeLongstotheclassitself, nottoinstances.StaticvariablesAresharedAcrossAllInstances ? OutObjectCreation, ??? ForgloBalTrackingorConstants.StaticMethodsOperateateAteAteClassEvel, canceCcessnon-StaticMbers, statice
