1ãéè¿ç»§æ¿Threadç±»å建线ç¨
(1).é¦å
å®ä¹ä¸ä¸ªç±»å»ç»§æ¿Threadç¶ç±»ï¼éåç¶ç±»ä¸çrun()æ¹æ³ãå¨run()æ¹æ³ä¸å å
¥å
·ä½çä»»å¡ä»£ç æå¤çé»è¾ã
(2).ç´æ¥å建ä¸ä¸ªThreadTestç±»ç对象ï¼ä¹å¯ä»¥å©ç¨å¤ææ§ï¼åé声æ为ç¶ç±»çç±»åã
(3).è°ç¨startæ¹æ³ï¼çº¿ç¨å¯å¨ï¼éå«çè°ç¨run()æ¹æ³ã
[java] view plain copy
public class ThreadTest extends Thread{
public void run(){
for(int i=0;i<=10;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
ThreadTest thread1=new ThreadTest();
ThreadTest thread2=new ThreadTest();
thread1.start();
thread2.start();
}
}
2ãéè¿å®ç°Runnableæ¥å£å建线ç¨
(1).å®ä¹ä¸ä¸ªç±»å®ç°Runnableæ¥å£ï¼éåæ¥å£ä¸çrun()æ¹æ³ãå¨run()æ¹æ³ä¸å å ¥å ·ä½çä»»å¡ä»£ç æå¤çé»è¾ã
(2).å建Runnableæ¥å£å®ç°ç±»ç对象ã
(3).å建ä¸ä¸ªThreadTestç±»ç对象ï¼éè¦å°è£ åé¢Runnableæ¥å£å®ç°ç±»ç对象ãï¼æ¥å£å¯ä»¥å®ç°å¤ç»§æ¿ï¼
(4).è°ç¨Thread对象çstart()æ¹æ³ï¼å¯å¨çº¿ç¨
[java] view plain copy
public class ThreadTest implements Runnable{
@Override
public void run() {
for(int i=0;i<=10;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
ThreadTest threadTest=new ThreadTest();
Thread theard=new Thread(threadTest);
theard.start();
}
}
3.éè¿CallableåFutureå建线ç¨
ï¼1ï¼å建Callableæ¥å£çå®ç°ç±»ï¼å¹¶å®ç°call()æ¹æ³ï¼è¯¥call()æ¹æ³å°ä½ä¸ºçº¿ç¨æ§è¡ä½ï¼å¹¶ä¸æè¿åå¼ã
ï¼2ï¼å建Callableå®ç°ç±»çå®ä¾ï¼ä½¿ç¨FutureTaskç±»æ¥å è£ Callable对象ï¼è¯¥FutureTask对象å°è£ äºè¯¥Callable对象çcall()æ¹æ³çè¿åå¼ã
ï¼3ï¼ä½¿ç¨FutureTask对象ä½ä¸ºThread对象çtargetå建并å¯å¨æ°çº¿ç¨ã
ï¼4ï¼è°ç¨FutureTask对象çget()æ¹æ³æ¥è·å¾å线ç¨æ§è¡ç»æåçè¿åå¼
[java] view plain copy
public class ThreadTest implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int count =0;
for(int i=0;i<=10;i++){
count=count+i;
}
return count;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ThreadTest test=new ThreadTest();
FutureTask<Integer> thread = new FutureTask<>(test);
new Thread(thread,"æè¿åå¼ç线ç¨").start();
System.out.println(thread.get());
}
}
使ç¨å®ç°Runnableæ¥å£æ¹å¼å建线ç¨å¯ä»¥å ±äº«åä¸ä¸ªç®æ 对象ï¼TreadDemo1 tt=new TreadDemo1();ï¼ï¼å®ç°äºå¤ä¸ªç¸å线ç¨å¤çåä¸ä»½èµæºã
ç¶ååçä¸æ®µæ¥èªJDKç解éï¼
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.
In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
éç¨å®ç°RunnableãCallableæ¥å£çæ¹å¼åè§å¤çº¿ç¨æ¶ï¼ä¼å¿æ¯ï¼
线ç¨ç±»åªæ¯å®ç°äºRunnableæ¥å£æCallableæ¥å£ï¼è¿å¯ä»¥ç»§æ¿å ¶ä»ç±»ã
å¨è¿ç§æ¹å¼ä¸ï¼å¤ä¸ªçº¿ç¨å¯ä»¥å ±äº«åä¸ä¸ªtarget对象ï¼æ以é常éåå¤ä¸ªç¸å线ç¨æ¥å¤çåä¸ä»½èµæºçæ åµï¼ä»èå¯ä»¥å°CPUã代ç åæ°æ®åå¼ï¼å½¢ææ¸ æ°ç模åï¼è¾å¥½å°ä½ç°äºé¢å对象çææ³ã
å£å¿æ¯ï¼
ç¼ç¨ç¨å¾®å¤æï¼å¦æè¦è®¿é®å½å线ç¨ï¼åå¿ é¡»ä½¿ç¨Thread.currentThread()æ¹æ³ãéç¨ç»§æ¿Threadç±»æ¹å¼ï¼
ï¼1ï¼ä¼ç¹ï¼ç¼åç®åï¼å¦æéè¦è®¿é®å½å线ç¨ï¼æ é使ç¨Thread.currentThread()æ¹æ³ï¼ç´æ¥ä½¿ç¨thisï¼å³å¯è·å¾å½å线ç¨ã
ï¼2ï¼ç¼ºç¹ï¼å 为线ç¨ç±»å·²ç»ç»§æ¿äºThreadç±»ï¼æ以ä¸è½å继æ¿å
¶ä»çç¶ç±»ã
éç¨å®ç°Runnableæ¥å£æ¹å¼ï¼
ï¼1ï¼ä¼ç¹ï¼çº¿ç¨ç±»åªæ¯å®ç°äºRunableæ¥å£ï¼è¿å¯ä»¥ç»§æ¿å
¶ä»çç±»ãå¨è¿ç§æ¹å¼ä¸ï¼å¯ä»¥å¤ä¸ªçº¿ç¨å
±äº«åä¸ä¸ªç®æ 对象ï¼æ以é常éåå¤ä¸ªç¸å线ç¨æ¥å¤çåä¸ä»½èµæºçæ
åµï¼ä»èå¯ä»¥å°CPU代ç åæ°æ®åå¼ï¼å½¢ææ¸
æ°ç模åï¼è¾å¥½å°ä½ç°äºé¢å对象çææ³ã
ï¼2ï¼ç¼ºç¹ï¼ç¼ç¨ç¨å¾®å¤æï¼å¦æéè¦è®¿é®å½å线ç¨ï¼å¿
须使ç¨Thread.currentThread()æ¹æ³ã