遍匈 / Java縮殻 /

Java - 謹?殻園殻

<貧匯准 和匯准>
Java公謹?殻園殻戻工阻坪崔議屶隔。匯倖謹?殻殻會淫根曾倖賜謹倖嬬旺窟塰佩議何蛍。殻會議耽匯何蛍脅各恬匯倖?殻?旺拝耽倖?殻協吶阻匯倖鏡羨議峇佩揃抄。
謹?殻頁謹販暦議匯嶽蒙艶議侘塀。謹?殻曳謹販暦俶勣厚弌議蝕?。
宸戦協吶才?殻屢購議総匯倖宝囂?序殻?匯倖序殻淫凄喇荷恬狼由蛍塘議坪贋腎寂?淫根匯倖賜謹倖?殻。匯倖?殻音嬬鏡羨議贋壓?万駅倬頁序殻議匯何蛍。匯倖序殻匯岷塰佩?岷欺侭嗤議掲便昨?殻脅潤崩塰佩朔嘉嬬潤崩。
謹?殻嬬諾怎殻會埀園亟掲械嗤丼楕議殻會栖器欺割蛍旋喘CPU議朕議?咀葎CPU議腎椀扮寂嬬校隠隔壓恷詰尫業。
匯倖?殻議伏凋巓豚?
?殻将狛凪伏凋巓豚議光倖竣粁。和夕?塋樵牧燦氐潦麺衲蔞追翺迮榮據? 斤貧峰竣粁序佩傍苧?
  • 仟彜蓑: 匯倖仟恢伏議?殻貫仟彜蓑蝕兵阻万議伏凋巓豚。万隠隔宸倖彜蓑岑祇殻會start宸倖?殻。
  • 塰佩彜蓑: .輝匯倖仟彜蓑議?殻瓜start參朔??殻祥延撹辛塰佩彜蓑?匯倖?殻壓緩彜蓑和瓜範葎頁蝕兵峇佩凪販暦
  • 祥偖彜蓑: 輝匯倖?殻吉棋総翌匯倖?殻峇佩匯倖販暦議扮昨?乎?殻祥序秘祥偖彜蓑。輝総匯倖?殻公祥偖彜蓑議?殻窟僕佚催扮?乎?殻嘉嶷仟俳算欺塰佩彜蓑。
  • 俚蓄彜蓑: 喇噐匯倖?殻議扮寂頭喘頼阻?乎?殻貫塰佩彜蓑序秘俚蓄彜蓑。輝扮寂寂侯欺豚賜宀吉棋議扮寂窟伏阻?乎彜蓑議?殻俳算欺塰佩彜蓑。
  • 嶮峭彜蓑: 匯倖塰佩彜蓑議?殻頼撹販暦賜宀凪麿嶮峭訳周窟伏?乎?殻祥俳算欺嶮峭彜蓑。

?殻議單枠雫:

耽匯倖Java?殻脅嗤匯倖單枠雫?宸劔嗤廁噐荷恬狼由鳩協?殻議距業乏會。Java單枠雫壓MIN_PRIORITY?1?才MAX_PRIORITY?10?岻寂議袈律坪。潮範秤趨和?耽匯倖?殻脅氏蛍塘匯倖單枠雫NORM_PRIORITY?5?。
醤嗤熟互單枠雫議?殻斤殻會厚嶷勣?旺拝哘乎壓詰單枠雫議?殻岻念蛍塘侃尖匂扮寂。隼遇??殻單枠雫音嬬隠屬?殻峇佩議乏會?遇拝掲械卆正噐峠岬。
幹秀匯倖?殻:
Java戻工阻曾嶽幹秀?殻圭隈?
  • 宥狛糞孖Runable俊笥?
  • 宥狛写覚Thread窃云附。

宥狛糞孖Runnable俊笥栖幹秀?殻:

幹秀匯倖?殻?恷酒汽議圭隈頁幹秀匯倖糞孖Runnable俊笥議窃。
葎阻糞孖Runnable?匯倖窃峪俶勣峇佩匯倖圭隈距喘run()?蕗苧泌和?

publicvoid run()

低辛參嶷亟乎圭隈?嶷勣議頁尖盾議run()辛參距喘凪麿圭隈?聞喘凪麿窃?旺蕗苧延楚?祥崧麼?殻匯劔。
壓幹秀匯倖糞孖Runnable俊笥議窃岻朔?低辛參壓窃嶄糞箭晒匯倖?殻斤嵆。
Thread協吶阻叱倖更夛圭隈?和中議宸倖頁厘断将械聞喘議?

Thread(Runnable threadOb,String threadName);

宸戦?threadOb 頁匯倖糞孖Runnable 俊笥議窃議糞箭?旺拝 threadName峺協仟?殻議兆忖。
仟?殻幹秀岻朔?低距喘万議start()圭隈万嘉氏塰佩。

void start();

糞箭:

和中頁匯倖幹秀?殻旺蝕兵斑万峇佩議糞箭?

// Create a new thread.
class NewThread implements Runnable {
 Thread t;
 NewThread() {
 // Create a new, second thread
 t = new Thread(this, "Demo Thread");
 System.out.println("Child thread: " + t);
 t.start(); // Start the thread
 }
 
 // This is the entry point for the second thread.
 public void run() {
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println("Child Thread: " + i);
 // Let the thread sleep for a while.
 Thread.sleep(50);
 }
 } catch (InterruptedException e) {
 System.out.println("Child interrupted.");
 }
 System.out.println("Exiting child thread.");
 }
}
 
public class ThreadDemo {
 public static void main(String args[]) {
 new NewThread(); // create a new thread
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println("Main Thread: " + i);
 Thread.sleep(100);
 }
 } catch (InterruptedException e) {
 System.out.println("Main thread interrupted.");
 }
 System.out.println("Main thread exiting.");
 }
}
 

塰佩潤惚泌和?

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

宥狛写覚Thread栖幹秀?殻:

幹秀匯倖?殻議及屈嶽圭隈頁幹秀匯倖仟議窃?乎窃写覚Thread窃?隼朔幹秀匯倖乎窃議糞箭。
写覚窃駅倬嶷亟run()圭隈?乎圭隈頁仟?殻議秘笥泣。万匆駅倬距喘start()圭隈嘉嬬峇佩。

糞箭:

// Create a second thread by extending Thread
class NewThread extends Thread {
 NewThread() {
 // Create a new, second thread
 super("Demo Thread");
 System.out.println("Child thread: " + this);
 start(); // Start the thread
 }
 
 // This is the entry point for the second thread.
 public void run() {
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println("Child Thread: " + i);
 // Let the thread sleep for a while.
 Thread.sleep(50);
 }
 } catch (InterruptedException e) {
 System.out.println("Child interrupted.");
 }
 System.out.println("Exiting child thread.");
 }
}
 
public class ExtendThread {
 public static void main(String args[]) {
 new NewThread(); // create a new thread
 try {
 for(int i = 5; i > 0; i--) {
 System.out.println("Main Thread: " + i);
 Thread.sleep(100);
 }
 } catch (InterruptedException e) {
 System.out.println("Main thread interrupted.");
 }
 System.out.println("Main thread exiting.");
 }
}
 

塰佩潤惚泌和?

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.

Thread 圭隈:

和燕双竃阻Thread窃議匯乂嶷勣圭隈?

會催 圭隈宙峰
1 public void start()
聞乎?殻蝕兵峇佩?Java 倡亭字距喘乎?殻議 run 圭隈。
2 public void run()
泌惚乎?殻頁聞喘鏡羨議 Runnable 塰佩斤嵆更夛議?夸距喘乎 Runnable 斤嵆議 run 圭隈?倦夸?乎圭隈音峇佩販採荷恬旺卦指。
3 public final void setName(String name)
個延?殻兆各?聞岻嚥歌方 name 屢揖。
4 public final void setPriority(int priority)
厚個?殻議單枠雫。
5 public final void setDaemon(boolean on)
繍乎?殻炎芝葎便擦?殻賜喘薩?殻。
6 public final void join(long millisec)
吉棋乎?殻嶮峭議扮寂恷海葎 millis 坐昼。
7 public void interrupt()
嶄僅?殻。
8 public final boolean isAlive()
霞編?殻頁倦侃噐試強彜蓑。
貧峰圭隈頁瓜Thread斤嵆距喘議。和中議圭隈頁Thread窃議床蓑圭隈。
會催 圭隈宙峰
1 public static void yield()
壙唯輝念屎壓峇佩議?殻斤嵆?旺峇佩凪麿?殻。
2 public static void sleep(long millisec)
壓峺協議坐昼方坪斑輝念屎壓峇佩議?殻俚蓄?壙唯峇佩??緩荷恬鞭欺狼由柴扮匂才距業殻會娼業才彈鳩來議唹峒。
3 public static boolean holdsLock(Object x)
輝拝叙輝輝念?殻壓峺協議斤嵆貧隠隔酌篇匂迄扮?嘉卦指 true。
4 public static Thread currentThread()
卦指斤輝念屎壓峇佩議?殻斤嵆議哈喘。
5 public static void dumpStack()
繍輝念?殻議均媚効忸嬉咫崛炎彈危列送。

糞箭:

泌和議ThreadClassDemo 殻會處幣阻Thread窃議匯乂圭隈?
// File Name : DisplayMessage.java
// Create a thread to implement Runnable
public class DisplayMessage implements Runnable
{
 private String message;
 public DisplayMessage(String message)
 {
 this.message = message;
 }
 public void run()
 {
 while(true)
 {
 System.out.println(message);
 }
 }
}
 
// File Name : GuessANumber.java
// Create a thread to extentd Thread
public class GuessANumber extends Thread
{
 private int number;
 public GuessANumber(int number)
 {
 this.number = number;
 }
 public void run()
 {
 int counter = 0;
 int guess = 0;
 do
 {
 guess = (int) (Math.random() * 100 + 1);
 System.out.println(this.getName()
 + " guesses " + guess);
 counter++;
 }while(guess != number);
 System.out.println("** Correct! " + this.getName()
 + " in " + counter + " guesses.**");
 }
}
 
// File Name : ThreadClassDemo.java
public class ThreadClassDemo
{
 public static void main(String [] args)
 {
 Runnable hello = new DisplayMessage("Hello");
 Thread thread1 = new Thread(hello);
 thread1.setDaemon(true);
 thread1.setName("hello");
 System.out.println("Starting hello thread...");
 thread1.start();
 
 Runnable bye = new DisplayMessage("Goodbye");
 Thread thread2 = new Thread(hello);
 thread2.setPriority(Thread.MIN_PRIORITY);
 thread2.setDaemon(true);
 System.out.println("Starting goodbye thread...");
 thread2.start();
 
 System.out.println("Starting thread3...");
 Thread thread3 = new GuessANumber(27);
 thread3.start();
 try
 {
 thread3.join();
 }catch(InterruptedException e)
 {
 System.out.println("Thread interrupted.");
 }
 System.out.println("Starting thread4...");
 Thread thread4 = new GuessANumber(75);
 
 thread4.start();
 System.out.println("main() is ending...");
 }
}

塰佩潤惚泌和?耽匯肝塰佩議潤惚脅音匯劔。
Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Thread-2 guesses 27
Hello
** Correct! Thread-2 in 102 guesses.**
Hello
Starting thread4...
Hello
Hello
..........remaining result produced.

?殻議叱倖麼勣古廷:

壓謹?殻園殻扮?低俶勣阻盾參和叱倖古廷?

謹?殻議聞喘:

嗤丼旋喘謹?殻議購囚頁尖盾殻會頁旺窟峇佩遇音頁堪佩峇佩議。箭泌?殻會嶄嗤曾倖徨狼由俶勣旺窟峇佩?宸扮昨祥俶勣旋喘謹?殻園殻。
宥狛斤謹?殻議聞喘?辛參園亟竃掲械互丼議殻會。音狛萩廣吭?泌惚低幹秀湊謹議?殻?殻會峇佩議丼楕糞縞貧頁週詰阻?遇音頁戻幅阻。
萩芝廖?貧和猟議俳算蝕?匆載嶷勣?泌惚低幹秀阻湊謹議?殻?CPU雑継壓貧和猟議俳算議扮寂繍謹噐峇佩殻會議扮寂?
<貧匯准 和匯准>

AltStyle によって変換されたページ (->オリジナル) /