1.Javaä¸RunnableåThreadçåºå«
Javaä¸RunnableåThreadçåºå«
nableåthreadçåºå«ï¼å¤çº¿ç¨å¿ é¡»ç¨Runableï¼
Javaä¸æ两ç§å®ç°å¤çº¿ç¨çæ¹å¼ä»¥å两ç§æ¹å¼ä¹é´çåºå«
çå°ä¸ä¸ªé¢è¯é¢.é®ä¸¤ç§å®ç°å¤çº¿ç¨çæ¹æ³.没äºå»ç½ä¸æ¾äºæ¾çæ¡.
ç½ä¸æµä¼ å¾å¹¿çæ¯ä¸ä¸ªç½ä¸å®ç¥¨ç³»ç»è®²è§£.转åè¿æ¥.å·²ç»ä¸ç¥éåæå°åºæ¯åºèªåªéäº.
Javaä¸æ两ç§å®ç°å¤çº¿ç¨çæ¹å¼ãä¸æ¯ç´æ¥ç»§æ¿Threadç±»ï¼äºæ¯å®ç°Runnableæ¥å£ãé£ä¹è¿ä¸¤ç§å®ç°å¤çº¿ç¨çæ¹å¼å¨åºç¨ä¸æä»ä¹åºå«å¢ï¼
为äºåçè¿ä¸ªé®é¢ï¼æ们å¯ä»¥éè¿ç¼åä¸æ®µä»£ç æ¥è¿è¡åæãæ们ç¨ä»£ç æ¥æ¨¡æéè·¯å®ç¥¨ç³»ç»ï¼å®ç°éè¿å个å®ç¥¨ç¹åå®ææ¥æ次å车çå¼ è½¦ç¥¨ï¼ä¸ä¸ªå®ç¥¨ç¹ç¨ä¸ä¸ªçº¿ç¨è¡¨ç¤ºã
é¦å è¿æ ·ç¼åè¿ä¸ªç¨åºï¼
Java代ç
class ThreadTest extends Thread{
private int ticket = ;
public void run(){
while(true){
if(ticket > 0){
System.out.println(Thread.currentThread().getName() +
"is saling ticket" + ticket--);
}else{
break;
}
}
}
}
æºç æå°ï¼
class ThreadTest extends Thread{
private int ticket = ;
public void run(){
while(true){
if(ticket > 0){
System.out.println(Thread.currentThread().getName() +
"is saling ticket" + ticket--);
}else{
break;
}
}
}
}
mainæµè¯ç±»:
Java代ç
public class ThreadDome1{
public static void main(String[] args){
ThreadTest t = new ThreadTest();
t.start();
t.start();
t.start();
t.start();
}
}
æºç æå°ï¼
public class ThreadDome1{
public static void main(String[] args){
ThreadTest t = new ThreadTest();
t.start();
t.start();
t.start();
t.start();
}
}
ä¸é¢ç代ç ä¸ï¼æ们ç¨ThreadTest类模æå®ç¥¨å¤çå®ç¥¨è¿ç¨ï¼runæ¹æ³ä¸çæ¯ä¸æ¬¡å¾ªç¯é½å°æ»ç¥¨æ°å1ï¼æ¨¡æååºä¸å¼ 车票ï¼åæ¶è¯¥è½¦ç¥¨å·æå°åºæ¥ï¼ç´æ¥å©ä½ç票æ°å°é¶ä¸ºæ¢ãå¨ThreadDemo1ç±»çmainæ¹æ³ä¸ï¼æ们å建äºä¸ä¸ªçº¿ç¨å¯¹è±¡ï¼å¹¶éå¤å¯å¨å次ï¼å¸æéè¿è¿ç§æ¹å¼äº§çå个线ç¨ãä»è¿è¡çç»ææ¥çæ们åç°å ¶å®åªæä¸ä¸ªçº¿ç¨å¨è¿è¡ï¼è¿ä¸ªç»æ åè¯æ们ï¼ä¸ä¸ªçº¿ç¨å¯¹è±¡åªè½å¯å¨ä¸ä¸ªçº¿ç¨ï¼æ è®ºä½ è°ç¨å¤å°éstart()æ¹æ³ï¼ç»æåªæä¸ä¸ªçº¿ç¨ã
æ们æ¥çä¿®æ¹ThreadDemo1ï¼å¨mainæ¹æ³ä¸å建å个Thread对象ï¼
Java代ç
public class ThreadDemo1{
public static void main(String[] args){
new ThreadTest().start();
new ThreadTest().start();
new ThreadTest().start();
new ThreadTest().start();
}
}
æºç æå°ï¼
public class ThreadDemo1{
public static void main(String[] args){
new ThreadTest().start();
new ThreadTest().start();
new ThreadTest().start();
new ThreadTest().start();
}
}
Java代ç
class ThreadTest extends Thread{
private int ticket = ;
public void run(){
while(true){
if(ticket > 0){
System.out.println(Thread.currentThread().getName() +
" is saling ticket" + ticket--);
}else{
break;
}
}
}
}
æºç æå°ï¼
class ThreadTest extends Thread{
private int ticket = ;
public void run(){
while(true){
if(ticket > 0){
System.out.println(Thread.currentThread().getName() +
" is saling ticket" + ticket--);
}else{
break;
}
}
}
}
è¿ä¸è¾¾å°ç®çäºåï¼
ä»ç»æä¸çæ¯ä¸ªç¥¨å·é½è¢«æå°äºå次ï¼å³ å个线ç¨åèªååèªçå¼ ç¥¨ï¼èä¸å»åå ±åçå¼ ç¥¨ãè¿ç§æ åµæ¯æä¹é æçå¢ï¼æ们éè¦çæ¯ï¼å¤ä¸ªçº¿ç¨å»å¤çåä¸ä¸ªèµæºï¼ä¸ä¸ªèµæºåªè½å¯¹åºä¸ä¸ªå¯¹è±¡ï¼å¨ä¸é¢çç¨åºä¸ï¼æ们å建äºå个ThreadTest对象ï¼å°±çäºå建äºå个èµæºï¼æ¯ä¸ªèµæºé½æå¼ ç¥¨ï¼æ¯ä¸ªçº¿ç¨é½å¨ç¬èªå¤çåèªçèµæºã
ç»è¿è¿äºå®éªååæï¼å¯ä»¥æ»ç»åºï¼è¦å®ç°è¿ä¸ªéè·¯å®ç¥¨ç¨åºï¼æ们åªè½å建ä¸ä¸ªèµæºå¯¹è±¡ï¼ä½è¦å建å¤ä¸ªçº¿ç¨å»å¤çåä¸ä¸ªèµæºå¯¹è±¡ï¼å¹¶ä¸æ¯ä¸ªçº¿ç¨ä¸æè¿è¡çæ¯ç¸åçç¨åºä»£ç ãå¨å顾ä¸ä¸ä½¿ç¨æ¥å£ç¼åå¤çº¿ç¨çè¿ç¨ã
Java代ç
public class ThreadDemo1{
public static void main(String[] args){
ThreadTest t = new ThreadTest();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
æºç æå°ï¼
public class ThreadDemo1{
public static void main(String[] args){
ThreadTest t = new ThreadTest();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
Java代ç
class ThreadTest implements Runnable{
private int tickets = ;
public void run(){
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() +
" is saling ticket " + tickets--);
}
}
}
}
æºç æå°ï¼
class ThreadTest implements Runnable{
private int tickets = ;
public void run(){
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() +
" is saling ticket " + tickets--);
}
}
}
}
ä¸é¢çç¨åºä¸ï¼å建äºå个线ç¨ï¼ æ¯ä¸ªçº¿ç¨è°ç¨çæ¯åä¸ä¸ªThreadTest对象ä¸çrun()æ¹æ³ï¼è®¿é®çæ¯åä¸ä¸ªå¯¹è±¡ä¸çåéï¼ticketsï¼çå®ä¾ï¼è¿ä¸ªç¨åºæ»¡è¶³äºæ们çéæ±ãå¨Windowsä¸å¯ä»¥å¯å¨å¤ä¸ªè®°äºæ¬ç¨åºä¸æ ·ï¼ä¹å°±æ¯å¤ä¸ªè¿ç¨ä½¿ç¨åä¸ä¸ªè®°äºæ¬ç¨åºä»£ç ã
å¯è§ï¼ å®ç°Runnableæ¥å£ç¸å¯¹äºç»§æ¿Threadç±»æ¥è¯´ï¼æå¦ä¸æ¾èç好å¤ï¼
(1)éåå¤ä¸ªç¸åç¨åºä»£ç ç线ç¨å»å¤çåä¸èµæºçæ åµï¼æèæCPUï¼çº¿ç¨ï¼åç¨åºç代ç ï¼æ°æ®ææçå离ï¼è¾å¥½å°ä½ç°äºé¢å对象ç设计ææ³ã
(2)å¯ä»¥é¿å ç±äºJavaçå继æ¿ç¹æ§å¸¦æ¥çå±éãæ们ç»å¸¸ç¢°å°è¿æ ·ä¸ç§æ åµï¼å³å½æ们è¦å°å·²ç»ç»§æ¿äºæä¸ä¸ªç±»çåç±»æ¾å ¥å¤çº¿ç¨ä¸ï¼ç±äºä¸ä¸ªç±»ä¸è½åæ¶æ两个ç¶ç±»ï¼æ以ä¸è½ç¨ç»§æ¿Threadç±»çæ¹å¼ï¼é£ä¹ï¼è¿ä¸ªç±»å°±åªè½éç¨å®ç°Runnableæ¥å£çæ¹å¼äºã
(3)æå©äºç¨åºçå¥å£®æ§ï¼ä»£ç è½å¤è¢«å¤ä¸ªçº¿ç¨å ±äº«ï¼ä»£ç ä¸æ°æ®æ¯ç¬ç«çãå½å¤ä¸ªçº¿ç¨çæ§è¡ä»£ç æ¥èªåä¸ä¸ªç±»çå®ä¾æ¶ï¼å³ç§°å®ä»¬å ±äº«ç¸åç代ç ãå¤ä¸ªçº¿ç¨æä½ç¸åçæ°æ®ï¼ä¸å®ä»¬ç代ç æ å ³ãå½å ±äº«è®¿é®ç¸åç对象æ¯ï¼å³å®ä»¬å ±äº«ç¸åçæ°æ®ãå½çº¿ç¨è¢«æé æ¶ï¼éè¦ç代ç åæ°æ®éè¿ä¸ä¸ªå¯¹è±¡ä½ä¸ºæé å½æ°å®åä¼ éè¿å»ï¼è¿ä¸ªå¯¹è±¡å°±æ¯ä¸ä¸ªå®ç°äºRunnableæ¥å£çç±»çå®ä¾ã Javaä¸RunnableåThreadçåºå«æ´è¯¦ç»çèµæåèï¼/course/course_id-.html
2度交保、發回更裁!鄭文燦涉貪3進羈押庭 面色凝重嚴肅
获取网站源码如何找_获取网站源码如何找到
html折线图源码_html折线图代码
商城合伙人源码_商城合伙人源码是什么
市售鮮奶茶調查 知名手搖標「小農鮮奶」卻用別牌、還有濃縮乳加水
小程序diy相册源码_小程序diy相册源码是什么