java创建线程的四种方式

参考自:http://blog.csdn.net/longshengguoji/article/details/41126119

java创建线程的三种方式

1. 继承Thread类创建线程类

    package com.thread;  
      
    public class FirstThreadTest extends Thread{  
        int i = 0;  
        //重写run方法,run方法的方法体就是现场执行体  
        public void run()  
        {  
            for(;i<100;i++){  
            System.out.println(getName()+"  "+i);  
              
            }  
        }  
        public static void main(String[] args)  
        {  
            for(int i = 0;i< 100;i++)  
            {  
                System.out.println(Thread.currentThread().getName()+"  : "+i);  
                if(i==20)  
                {  
                    new FirstThreadTest().run();  
                    new FirstThreadTest().run();  
                }  
            }  
        }  
      
    }  


2. 通过Runable接口创建线程类

    package com.thread;  
      
    public class RunnableThreadTest implements Runnable  
    {  
      
        private int i;  
        public void run()  
        {  
            for(i = 0;i <100;i++)  
            {  
                System.out.println(Thread.currentThread().getName()+" "+i);  
            }  
        }  
        public static void main(String[] args)  
        {  
            for(int i = 0;i < 100;i++)  
            {  
                System.out.println(Thread.currentThread().getName()+" "+i);  
                if(i==20)  
                {  
                    RunnableThreadTest rtt = new RunnableThreadTest();  
                    new Thread(rtt,"新线程1").start();  
                    new Thread(rtt,"新线程2").start();  
                }  
            }  
      
        }  
      
    }  


3. 通过Callable和FutureTask创建线程

    a. 创建Callable接口的实现类,并实现call()方法;
    b. 创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callback对象的call()方法的返回值;
    c. 使用FutureTask对象作为Thread对象的target创建并启动新线程;

    d. 调用FutureTask对象的get()方法来获得子线程执行结束后的返回值。

package com.demo;

import java.util.concurrent.Callable;  
import java.util.concurrent.ExecutionException;  
import java.util.concurrent.FutureTask;  
  
public class CallableThreadTest implements Callable<Integer>  
{  
  
    public static void main(String[] args)  
    {  
        CallableThreadTest ctt = new CallableThreadTest();  
        FutureTask<Integer> ft = new FutureTask<Integer>(ctt);  
//        Thread thread = new Thread(ft,"有返回值的线程");
//        thread.start();
        for(int i = 0;i < 100;i++)  
        {  
            System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i);  
            if(i==20)  
            {  
                new Thread(ft,"有返回值的线程").start();  
            }  
        }  
        try  
        {  
            System.out.println("子线程的返回值:"+ft.get());  
        } catch (InterruptedException e)  
        {  
            e.printStackTrace();  
        } catch (ExecutionException e)  
        {  
            e.printStackTrace();  
        }  
  
    }  
  
    @Override  
    public Integer call() throws Exception  
    {  
        int i = 0;  
        for(;i<100;i++)  
        {  
            System.out.println(Thread.currentThread().getName()+" "+i);  
        }  
        return i;  
    }  
  
}  


4. 通过线程池创建线程

/**
 * 
 */
package com.demo;

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

/**
 * @author Maggie
 *
 */
public class ThreadPool 
{
	/* POOL_NUM */
	private static int POOL_NUM = 10;
	
	/**
	 * Main function
	 */
	public static void main(String[] args)
	{
		ExecutorService executorService = Executors.newFixedThreadPool(5);
		for(int i = 0; i<POOL_NUM; i++)
		{
			RunnableThread thread = new RunnableThread();
			executorService.execute(thread);
		}
	}
}

class RunnableThread implements Runnable
{
	private int THREAD_NUM = 10;
	public void run()
	{
		for(int i = 0; i<THREAD_NUM; i++)
		{
			System.out.println("线程" + Thread.currentThread() + " " + i);
		} 
	}
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值