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