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

java 线程

2013-10-22 15:24 344 查看
1  JAVA 的线程创建方法

1.1 继承Thread 方法

ThreadTest1tt = new ThreadTest1();

  tt.start();

1.2 实现runnable接口

    

public class mythread implements Runnable
{

public static void main(String[] args)
{
mythread thread = new mythread();
thread.run();
}

@Override
public void run() {
System.out.println("实现runnable接口开启线程成功");

}
}


2  Sleep,wait()之间的区别

Sleep 是线程进入”睡眠”状态,但是他并不会释放所占用的内存。Wait 是进入”等待”状态,会释放他所占用的内存,当调用其notify()或者notifyall()方法时,会退出等待状态,继续执行。

3  线程之间传递值的方法

3.1 主线程向子线程传递参数

3.1.1  通过构造函数进行

publicclass MyThread1 extends Thread
{
privateString name;
publicMyThread1(String name)
{
this.name= name;
}
publicvoid run()
{
System.out.println("hello" + name);
}
publicstatic void main(String[] args)
{
Threadthread = new MyThread1("world");
thread.start();
}
}


3.1.2  通过变量和方法传递参数

publicclass MyThread2 implements Runnable
{
privateString name;
publicvoid setName(String name)
{
this.name= name;
}
publicvoid run()
{
System.out.println("hello" + name);
}
publicstatic void main(String[] args)
{
MyThread2myThread = new MyThread2();
myThread.setName("world");
Threadthread = new Thread(myThread);
thread.start();
}
}


 

3.2 带有返回值的线程

在JDK5之前线程是没有返回值的,需要线程返回信息非常大费周章。

带有返回的任务必须实现Callable接口,无返回值的实现runnable接口

 

 

4  JAVA线程池

4.1什么是线程池

是一个一个可以容乃固定、可变的池,将线程放入到该线程池中,线程就会自动运行起来start(),减少系统创建线程的时间和开销,当线程过多时,线程会自动排队,当线程池中有执行完毕的线程过后,该线程就会放入线程池中运行起来,依次类推。这和连接池还是有区别的,连接池是当连接不够的时候,自动增加连接池里面的连接数目来满足系统的需要,而线程池需要排队等待。

4.2JAVA创建线程池

① 创建固定大小的线程

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPool extends Thread{

/**
* @param args
*/
public static voidmain(String[] args) {
//创建一个可重用固定线程数的线程池
ExecutorService pool= Executors.newFixedThreadPool(10);
mythread t1 = newmythread(0);
mythread t2 = newmythread(1);
mythread t3 = newmythread(2);
mythread t4 = newmythread(3);
mythread t5 = newmythread(4);
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
// 关闭线程池
pool.shutdown();
}

}

public class mythread implements Runnable
{
private int i;
/*    public static voidmain(String[] args)
{
mythread thread01 =new mythread(0);
thread01.run();
mythread thread02 =new mythread(1);
thread02.run();
}*/
mythread(int i){
this.i = i;
}
@Override
public void run() {
System.out.println("线程:"+i+"实现runnable接口开启线程成功");
try {
Thread.sleep(200);  //睡眠200毫秒主要是为了查看单任务线程和多任务线程之间的效果区别
} catch(InterruptedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}

}


② 创建单任务线程池

创建单任务线程只需将改成

ExecutorServicepool = Executors.newSingleThreadExecutor();


③ 创建可变线程

创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们

ExecutorServicepool = Executors.newCachedThreadPool();


5  线程并发量的控制

线程的并发量可以通过线程信号量Semaphore(信号灯)来控制,Semaphore可以分为单值和多值两种,前者只能被一个线程获得,后者可以被多个线程获得。

    

publicclass TestWait {
/**
* 并发量控制
* LCC
*/
publicstatic void main(String[] args) {
//线程池
ExecutorServiceexec = Executors.newCachedThreadPool();
//只能5个线程同时访问
finalSemaphore semp = new Semaphore(5);
for(intindex = 1 ; index < 21 ; index++){
finalint NO = index;
Runnablerun = new Runnable() {

@Override
publicvoid run() {
try{
//获取许可
semp.acquire();
System.out.println("Accessing:"+NO);
Thread.sleep(1000);
//访问完后,释放
System.out.println("Releasa:"+NO);
semp.release();
}catch (Exception e) {

}

}
};
exec.execute(run);
}
//关闭线程池
exec.shutdown();
}

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