java多线程的创建方法

一共四种创建线程的方法、以前两种,jdk1.5新增两种

1.继承Thread类实现多线程

public class MyThread extends Thread {
	public MyThread() {
		
	}
	public void run() {
		for(int i=0;i<10;i++) {
			System.out.println(Thread.currentThread()+":"+i);
		}
	}
	public static void main(String[] args) {
		MyThread mThread1=new MyThread();
		MyThread mThread2=new MyThread();
		MyThread myThread3=new MyThread();
		mThread1.start();
		mThread2.start();
		myThread3.start();
	}
}

2.覆写Runnable()接口实现多线程,而后同样覆写run().推荐此方式

public class MyThread implements Runnable{
	public static int count=20;
	public void run() {
		while(count>0) {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+count--);
		}
	}
	public static void main(String[] args) {
		MyThread Thread1=new MyThread();
		Thread mThread1=new Thread(Thread1,"线程1");
		Thread mThread2=new Thread(Thread1,"线程2");
		Thread mThread3=new Thread(Thread1,"线程3");
		mThread1.start();
		mThread2.start();
		myThread3.start();
	}
}

3.实现Callable 接口

package com.jerry.thread10;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadCallableTest {
    public static void main(String[] args) {
        //3.创建Callable接口实现类的对象
        NumThread numThread = new NumThread();

        //4.将此Callable实现类的对象(numThread)传递FutureTask构造其中,创建FutureTask对象。
        FutureTask<Integer> futureTask = new FutureTask<Integer>(numThread);

        //5.将FutureTask创建的对象,传递到Thread构造器中,在start该线程。
        new Thread(futureTask).start();

        //6.如果关心线程中call方法的返回值,则可以用个futureTask.get() 来抓取返回值。
        try {
            //拿到其返回值
            Integer sum = futureTask.get();
            System.out.println("总和为: " + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

//1.创建一个实现Callable 的实现类
class NumThread implements Callable<Integer> {

    //2.实现call方法,将此线程需要执行的操作声明到call方法中,注意该方法是有返回值的。
    @Override
    public Integer call() throws Exception {
        int sum = 0;

        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
                sum += i;
            }
        }

        return sum;
    }
}

4.用线程池(ThreadPool)

package com.jerry.thread10;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolTest {

    public static void main(String[] args) {
        //1.创建一个固定大小的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        //2.设置线程池属性
        ThreadPoolExecutor executor1 = (ThreadPoolExecutor) service;
        executor1.setCorePoolSize(10);

        //3.执行当前线程,适合用于Runnable,Callable 要用submit
        service.execute(new NumThread1());//执行线程1,用于输出偶数
        service.execute(new NumThread2());//执行线程2,用于输出奇数
        //4.关闭当前线程,线程池不用了,则关闭当前线程
        service.shutdown();
    }
}

class NumThread1 implements Runnable {
    //输出偶数
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ": " + i);
            }
        }
    }
}

class NumThread2 implements Runnable {
    //输出奇数
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (i % 2 != 0) {
                System.out.println(Thread.currentThread().getName() + ": " + i);
            }
        }
    }
}

转自:https://www.cnblogs.com/vpersie2008/p/12801097.html

posted @ 2021-01-20 10:15  永真  阅读(60)  评论(0)    收藏  举报