您的位置:首页 > 编程语言 > Java开发

Java Thread小结

2012-11-29 13:31 197 查看
1. 线程进程概念

多线程: 单个程序内部好象在同一时刻运行多种运算.

多进程: 同一时间内好象有多个程序运行.

2. 线程状态

2.1 创建状态

2.2 可运行状态

2.3 不可运行状态

2.4 死亡状态

2.1 创建状态(new
thread)

Thread TestThread = new TestThread();

没有被启动,仅仅是空的线程对象,系统不为它分配资源.

可调用方法:start() stop()

2.2
可运行状态(runnable)

TestThread.start();

该方法产生了运行这个线程所需的系统资源,安排其运行

并调用线程体run(),使程序处于可运行状态.

注意:并不一定在运行状态,也许线程还没真正执行.

2.3 不可运行状态(not
runnable)及其返回

(1)调用了sleep()>时间过去

(2)调用了suspend()>resume()

(3)调用了wait()>该条件变量所在对象notify()/notifyAll()

(4)输入输出流中发生线程阻塞->特定的I/O指令

2.4
死亡状态(dead)

自然撤消: run()方法中正常退出

主动停止: stop()方法停止当前线程

3. 线程体构造

3.1继承构造

3.2接口构造

3.1 继承构造

class TestThread extends Thread

{

public TestThread(String str){

super(str);

}

public void run(){

........

}

}

class Caller()

{

public static void main(String[] args) {

new TestThread("May 1st").start();

}

}

3.2 接口构造

class TestThread implements Runnable

{

public void run(){

........

}

}

class Caller(){

public static void main(String[] args){

TestThread test = new TestThread();

new Thread(test).start();

}

}

注意:实现Runnable仅仅意味着有一个run()方法,不具有天生的线程能力.

4. 其它

4.1 构造方法选择

当一个线程已继承了另一个类时,用Runnable()接口

4.2 实用方法

isAlive():线程已被启动并且未被终止,返回true.

当应用程序需要经常处理大量短周期线程时,采用线程池技术是一个很好的解决方案。以下是小的测试程序

package org.net9.thread;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

public class MainApp{

public static void main(String[] args) {

MainApp test = new MainApp(1000);

test.start();

test.close();

}

private int size = 0;

ExecutorService pool = null;

public MainTask(int size) {

this.size = size;

}

public void start() {

long start = System.currentTimeMillis();

try {

// create cache thread pool

pool = Executors.newCachedThreadPool();

for (int i = 0; i < size; i ) {

Thread.sleep(50);

// thread usage

// new Thread(new SubTask()).start();

// thread-pool usage

pool.execute(new SubTask());

}

} catch (InterruptedException ie) {

ie.printStackTrace();

}

long end = System.currentTimeMillis();

System.out.println("test cost " (end - start) " ms!");

}

public void close() {

// disable new tasks from being submitted

pool.shutdown();

try {

// wait a while for existing tasks to terminate

if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {

// cancel currently executing tasks

pool.shutdownNow();

// wait a while for tasks to respond to being cancelled

if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {

// thread pool cannot terminate

System.err.println("thread pool cannot terminate");

}

}

} catch (InterruptedException ie) {

// re-cancel if current thread also interrupted

pool.shutdownNow();

// preserve interrupt status

Thread.currentThread().interrupt();

}

}

}

package org.net9.thread;

public class SubThread implements Runnable {

public void run() {

try {

Thread.sleep(500);

} catch (InterruptedException ie) {

ie.printStackTrace();

}

}

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: