Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
About Thread

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

l          Threads(執行緒)

注意

l           main() method本身就是一個thread

l           程式進入main()之後,就有兩個thread了,main()garbage collection

l           一個thread是一個object,要產生一個thread必須透過java.lang.Thread這個class

l           run() method的語法為”public void run() { ... }”

l           wait(); notify();notifyAll();須在synchronized context下使用,不然會產生runtime exceptionIllegalMonitorStateException

l           sleep()會丟出InterruptedException,使用需有try…catch,不然就要declare throws InterruptedException

l           重覆呼叫同一執行緒的start(),會導致IllegalThreadStateException,為runtime exception

l           synchronized不可用在constroctor

產生

執行緒

implementing

Runnable interface

01 class MyThread implements Runnable {

02 public void run() { /*....*/ }

03 }

04 public class Test {

05 public static void main(String args[]) {

06        MyThread r = new MyThread();

07        Thread t = new Thread(r);

08        t.start();

09        //......

10  }

11 }

l           定義一個implement Runnable interfaceclass,且一定要implements run() method,並instantiates這個class,並將此classinstance傳給Thread()

l           Line 7”r”必須是an instance of Runnable,否則會compile error

l           Line 7thread “t”被建立後不會馬上執行,必須呼叫start()後才執行。

extends Thread

01 public class MyThread extends Thread {

02        public void run() { /*......*/ }

03        public static void main(String[] args) {

04              MyThread t = new MyThread();

05              t.start();

06        }

07 }

l          
定義一個”java.lang.Thread”subclassoverrides run() method,然後instantiates這個subclass

l           Line 1:要extends Thread class

l           Line 2:要override run() method,否則會是空的。

l           Line 4:要instantiates Threadsubclass

控制

執行緒

注意

l           threadnew出來後不會馬上執行run()method,必須執行start()後才會進入runnable狀態。

l           threadrunning state時所執行的是這個threadrun()method

l           threadrunnablerunning(實際在CPU執行)或反之是由JVMthread scheduler任意決定。

l           不管呼叫什麼,頂多只能讓狀態變成runnable,不可能馬上變成running

不可以

l           一個threadrun()method結束時,這個thread就進入dead state(死掉了),不可以再restart一個dead thread(即再呼叫start()method),否則會compile error

狀態

改變

l           thread遇到下列狀況時,會從running換到blocked state

n           Input/Ouput blocked

n           sleep()join()wait()

n           進入synchronized block,但此synchronized block所保護的object lock已經被其他thread取走。

method

method

Features

Method Type

Class

yield()

 

static

Thread

run()

instance

Thread

 

start()

 

instance

Thread

interrupt()

 

instance

Thread

sleep(ms)

會丟出InterruptException

mslong type,單位是millisecond

ms不可省略

static

Thread

join(ms)

會丟出InterruptException

mslong type,單位是millisecond

ms0或空值,則表等待的時間為永遠

instance

Thread

wait(ms)

會丟出InterruptException

mslong type,單位是millisecond

ms0或空值,則表等待的時間為永遠

須事先拿到this objectlock

instance

Object

notify()

須事先拿到this objectlock

instance

Object

notifyall()

須事先拿到this objectlock

instance

Object



来自:【 Garfield 的 SCJP 閱讀筆記 】


相关文章

相关软件