實(shí)例方法有:
checkAccess()
判定當前運行的線(xiàn)程是否有權修改該線(xiàn)程。
getContextClassLoader()
返回該線(xiàn)程的上下文 ClassLoader。
getId()
返回該線(xiàn)程的標識符
getName()
返回該線(xiàn)程的名稱(chēng)。
getPriority()
返回線(xiàn)程的優(yōu)先級。
isAlive()
測試線(xiàn)程是否處于活動(dòng)狀態(tài)。
start()
使該線(xiàn)程開(kāi)始執行;Java 虛擬機調用該線(xiàn)程的 run 方法。
run()
如果該線(xiàn)程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執行任何操作并返回。
。。。等等
類(lèi)方法:
最常用的有
sleep(long millis)
在指定的毫秒數內讓當前正在執行的線(xiàn)程休眠(暫停執行)。
sleep(long millis, int nanos)
在指定的毫秒數加指定的納秒數內讓當前正在執行的線(xiàn)程休眠(暫停執行)
currentThread()
返回對當前正在執行的線(xiàn)程對象的引用。
yield()
暫停當前正在執行的線(xiàn)程對象,并執行其他線(xiàn)程。
。。。等等
具體參考下面網(wǎng)址: java.io.Thread
一、繼承Thread類(lèi)創(chuàng )建線(xiàn)程子類(lèi)
1.在這子類(lèi)中重寫(xiě)run方法,在run方法內寫(xiě)線(xiàn)程任務(wù)代碼
2.創(chuàng )建該子類(lèi)實(shí)例,即是創(chuàng )建了一個(gè)線(xiàn)程實(shí)例
3.調用該實(shí)例的start方法來(lái)啟動(dòng)該線(xiàn)程
二、建一個(gè)類(lèi)去實(shí)現Runnable接口
1.該類(lèi)去實(shí)現接口的run方法,run方法內寫(xiě)線(xiàn)程任務(wù)代碼
2.創(chuàng )建該類(lèi)實(shí)例,把該實(shí)例當作一個(gè)標記target傳給Thread類(lèi),如:Thread t = new Thread(該類(lèi)實(shí)例);即創(chuàng )建一個(gè)線(xiàn)程對象
3.調用線(xiàn)程的star方法來(lái)啟用該線(xiàn)程
public class TestMain {
public static void main(String[] args) {
//調用線(xiàn)程1
new ThreadTest1().start();
//調用線(xiàn)程2
ThreadTest2 t2 = new ThreadTest2();
new Thread(t2).start();
}
}
//實(shí)現多線(xiàn)程方式1,通過(guò)繼承Thread類(lèi)來(lái)實(shí)現
class ThreadTest1 extends Thread{
public void run(){
System.out.println("執行線(xiàn)程1。");
}
}
//實(shí)現多線(xiàn)程方式2,通過(guò)實(shí)現Runnable接口來(lái)實(shí)現
class ThreadTest2 implements Runnable{
public void run(){
System.out.println("執行線(xiàn)程2。");
}
}
以前在遠標學(xué)過(guò)有三種:(1)繼承Thread類(lèi),重寫(xiě)run函數
創(chuàng )建:
class xx extends Thread{
public void run(){
Thread.sleep(1000) //線(xiàn)程休眠1000毫秒,sleep使線(xiàn)程進(jìn)入Block狀態(tài),并釋放資源
}}
開(kāi)啟線(xiàn)程:
對象.start() //啟動(dòng)線(xiàn)程,run函數運行
(2)實(shí)現Runnable接口,重寫(xiě)run函數
開(kāi)啟線(xiàn)程:
Thread t = new Thread(對象) //創(chuàng )建線(xiàn)程對象
t.start()
(3)實(shí)現Callable接口,重寫(xiě)call函數
Callable是類(lèi)似于Runnable的接口,實(shí)現Callable接口的類(lèi)和實(shí)現Runnable的類(lèi)都是可被其它線(xiàn)程執行的任務(wù)。
Callable和Runnable有幾點(diǎn)不同:
①Callable規定的方法是call(),而Runnable規定的方法是run().
②Callable的任務(wù)執行后可返回值,而Runnable的任務(wù)是不能返回值的
③call()方法可拋出異常,而run()方法是不能拋出異常的。
④運行Callable任務(wù)可拿到一個(gè)Future對象,Future表示異步計算的結果。它提供了檢查計算是否完成的方法,以等
待計算的完成,并檢索計算的結果.通過(guò)Future對象可了解任務(wù)執行情況,可取消任務(wù)的執行,還可獲取任務(wù)執行的結果
1、通過(guò)繼承Thread類(lèi)創(chuàng )建線(xiàn)程(1).首先定義一個(gè)類(lèi)去繼承Thread父類(lèi),重寫(xiě)父類(lèi)中的run()方法。
在run()方法中加入具體的任務(wù)代碼或處理邏輯。(2).直接創(chuàng )建一個(gè)ThreadTest類(lèi)的對象,也可以利用多態(tài)性,變量聲明為父類(lèi)的類(lèi)型。
(3).調用start方法,線(xiàn)程啟動(dòng),隱含的調用run()方法。[java] view plain copypublic 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、通過(guò)實(shí)現Runnable接口創(chuàng )建線(xiàn)程(1).定義一個(gè)類(lèi)實(shí)現Runnable接口,重寫(xiě)接口中的run()方法。
在run()方法中加入具體的任務(wù)代碼或處理邏輯。(2).創(chuàng )建Runnable接口實(shí)現類(lèi)的對象。
(3).創(chuàng )建一個(gè)ThreadTest類(lèi)的對象,需要封裝前面Runnable接口實(shí)現類(lèi)的對象。(接口可以實(shí)現多繼承)(4).調用Thread對象的start()方法,啟動(dòng)線(xiàn)程 [java] view plain copypublic 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.通過(guò)Callable和Future創(chuàng )建線(xiàn)程 (1)創(chuàng )建Callable接口的實(shí)現類(lèi),并實(shí)現call()方法,該call()方法將作為線(xiàn)程執行體,并且有返回值。
(2)創(chuàng )建Callable實(shí)現類(lèi)的實(shí)例,使用FutureTask類(lèi)來(lái)包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值。(3)使用FutureTask對象作為T(mén)hread對象的target創(chuàng )建并啟動(dòng)新線(xiàn)程。
(4)調用FutureTask對象的get()方法來(lái)獲得子線(xiàn)程執行結束后的返回值 [java] view plain copypublic class ThreadTest implements Callable{ @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 thread = new FutureTask(test); new Thread(thread,"有返回值的線(xiàn)程").start(); System.out.println(thread.get()); } } 使用實(shí)現Runnable接口方式創(chuàng )建線(xiàn)程可以共享同一個(gè)目標對象(TreadDemo1 tt=new TreadDemo1();),實(shí)現了多個(gè)相同線(xiàn)程處理同一份資源。然后再看一段來(lái)自JDK的解釋?zhuān)篢he 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.采用實(shí)現Runnable、Callable接口的方式創(chuàng )見(jiàn)多線(xiàn)程時(shí),優(yōu)勢是:線(xiàn)程類(lèi)只是實(shí)現了Runnable接口或Callable接口,還可以繼承其他類(lèi)。
在這種方式下,多個(gè)線(xiàn)程可以共享同一個(gè)target對象,所以非常適合多個(gè)相同線(xiàn)程來(lái)處理同一份資源的情況,從而可以將CPU、代碼和數據分開(kāi),形成清晰的模型,較好地體現了面向對象的思想。劣勢是:編程稍微復雜,如果要訪(fǎng)問(wèn)當前線(xiàn)程,則必須使用Thread.currentThread()方法。
采用繼承Thread類(lèi)方式:(1)優(yōu)點(diǎn):編寫(xiě)簡(jiǎn)單,如果需要訪(fǎng)問(wèn)當前線(xiàn)程,無(wú)需使用Thread.currentThread()方法,直接使用this,即可獲得當前線(xiàn)程。(2)缺點(diǎn):因為線(xiàn)程類(lèi)已經(jīng)繼承了Thread類(lèi),所以不能再繼承其他的父類(lèi)。
采用實(shí)現Runnable接口方式:(1)優(yōu)點(diǎn):線(xiàn)程類(lèi)只是實(shí)現了Runable接口,還可以繼承其他的類(lèi)。在這種方式下,可以多個(gè)線(xiàn)程共享同一個(gè)目標對象,所以非常適合多個(gè)相同線(xiàn)程來(lái)處理同一份資源的情況,從而可以將CPU代碼和數據分開(kāi),形成清晰的模型,較好地體現了面向對象的思想。
(2)缺點(diǎn):編程稍微復雜,如果需要訪(fǎng)問(wèn)當前線(xiàn)程,必須使用Thread.currentThread()方法。
你new一個(gè)線(xiàn)程,右鍵下很多屬性的,網(wǎng)上也很多比如
Priority 線(xiàn)程的優(yōu)先級
Name線(xiàn)程的名字
ManagedThreadId 線(xiàn)程的唯一標識
Start啟動(dòng)線(xiàn)程
Resume 喚醒以?huà)炱鸬木€(xiàn)程
Suspend掛起線(xiàn)程
其中Sleep()是靜態(tài)方法只能通過(guò)類(lèi)調用。
線(xiàn)程的用處,最好的就是多線(xiàn)程啦,對于winform程序來(lái)說(shuō),多線(xiàn)程適解決winform程序ui界面體驗的最佳選擇了。線(xiàn)程可以提高性能,但是太多線(xiàn)程反而會(huì )影響性能,管理線(xiàn)程是比較麻煩的事
Java多線(xiàn)程的創(chuàng )建及啟動(dòng)
Java中線(xiàn)程的創(chuàng )建常見(jiàn)有如三種基本形式
1.繼承Thread類(lèi),重寫(xiě)該類(lèi)的run()方法。
復制代碼
1 class MyThread extends Thread {
2
3 private int i = 0;
4
5 @Override
6 public void run() {
7 for (i = 0; i myCallable = new MyCallable(); // 創(chuàng )建MyCallable對象
6 FutureTaskft = new FutureTask(myCallable); //使用FutureTask來(lái)包裝MyCallable對象
7
8 for (int i = 0; i {
32 private int i = 0;
33
34 // 與run()方法不同的是,call()方法具有返回值
35 @Override
36 public Integer call() {
37 int sum = 0;
38 for (; i implements RunnableFuture{
2
3 //。.
4
5 }
1 public interface RunnableFutureextends Runnable, Future{
2
3 void run();
4
5 }
于是,我們發(fā)現FutureTask類(lèi)實(shí)際上是同時(shí)實(shí)現了Runnable和Future接口,由此才使得其具有Future和Runnable雙重特性。通過(guò)Runnable特性,可以作為T(mén)hread對象的target,而Future特性,使得其可以取得新創(chuàng )建線(xiàn)程中的call()方法的返回值。
執行下此程序,我們發(fā)現sum = 4950永遠都是最后輸出的。而“主線(xiàn)程for循環(huán)執行完畢..”則很可能是在子線(xiàn)程循環(huán)中間輸出。由CPU的線(xiàn)程調度機制,我們知道,“主線(xiàn)程for循環(huán)執行完畢..”的輸出時(shí)機是沒(méi)有任何問(wèn)題的,那么為什么sum =4950會(huì )永遠最后輸出呢?
原因在于通過(guò)ft.get()方法獲取子線(xiàn)程call()方法的返回值時(shí),當子線(xiàn)程此方法還未執行完畢,ft.get()方法會(huì )一直阻塞,直到call()方法執行完畢才能取到返回值。
上述主要講解了三種常見(jiàn)的線(xiàn)程創(chuàng )建方式,對于線(xiàn)程的啟動(dòng)而言,都是調用線(xiàn)程對象的start()方法,需要特別注意的是:不能對同一線(xiàn)程對象兩次調用start()方法。
你好,本題已解答,如果滿(mǎn)意
請點(diǎn)右下角“采納答案”。
1、添加線(xiàn)程相關(guān)的頭文件:#include<pthread.h>
2、線(xiàn)程創(chuàng )建函數是pthread_create()函數,該函數的原型為:
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void* (*start_routine)(void*),void *arg);
3、線(xiàn)程退出函數是pthread_exit()函數,該函數的原型為:
void pthread_exit(void *retval);
創(chuàng )建線(xiàn)程的示例程序如下:
/***程序說(shuō)明:創(chuàng )建線(xiàn)程函數pthread_create()函數的使用。*/#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>#include <string.h>; //打印標識符的函數void print_ids(const char *str){ pid_t pid; //進(jìn)程標識符 pthread_t tid; //線(xiàn)程標識符 pid=getpid(); //獲得進(jìn)程號 tid=pthread_self(); //獲得線(xiàn)程號 printf("%s pid:%u tid:%u (0x%x)\n", str,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid); //打印進(jìn)程號和線(xiàn)程號} //線(xiàn)程函數void* pthread_func(void *arg){ print_ids("new thread:"); //打印新建線(xiàn)程號 return ((void*)0);} //主函數int main(){ int err; pthread_t ntid; //線(xiàn)程號 err=pthread_create(&ntid,NULL,pthread_func,NULL); //創(chuàng )建一個(gè)線(xiàn)程 if(err != 0) { printf("create thread failed:%s\n",strerror(err)); exit(-1); } print_ids("main thread:"); //打印主線(xiàn)程號 sleep(2); return 0;}
java創(chuàng )建線(xiàn)程的方式有三種
第一種是繼承Thread類(lèi) 實(shí)現方法run() 不可以?huà)伄惓?無(wú)返回值
第二種是實(shí)現Runnable接口 實(shí)現方法run() 不可以?huà)伄惓?無(wú)返回值
第三種是實(shí)現Callable<T>;接口,接口中要覆蓋的方法是 public <T> call() 注意:此方法可以?huà)伄惓#皟煞N不能 而且此方法可以有返回值
第三種如何運行呢 Callable接口在util.concurrent包中,由線(xiàn)程池提交
import java.util.concurrent.*;
ExecutorService e = Executors.newFixedThreadPool(10); 參數表示最多可以運行幾個(gè)線(xiàn)程
e.submit(); 這個(gè)里面參數傳 實(shí)現Callable接口那個(gè)類(lèi)的對象
聲明:本網(wǎng)站尊重并保護知識產(chǎn)權,根據《信息網(wǎng)絡(luò )傳播權保護條例》,如果我們轉載的作品侵犯了您的權利,請在一個(gè)月內通知我們,我們會(huì )及時(shí)刪除。
蜀ICP備2020033479號-4 Copyright ? 2016 學(xué)習?shū)B(niǎo). 頁(yè)面生成時(shí)間:2.617秒