java 多线程实现四种方式解析Thread,Runnable,Callable,ServiceExcutor,Synchronized ,ReentrantLock

Posted 入门到放弃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 多线程实现四种方式解析Thread,Runnable,Callable,ServiceExcutor,Synchronized ,ReentrantLock相关的知识,希望对你有一定的参考价值。

1.Thread实现:

import java.util.Date;
import java.text.SimpleDateFormat;

public class MyThread extends Thread{
    @Override
    public   void run(){

        SimpleDateFormat strf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String d = strf.format(new Date());// new Date()为获取当前系统时间
        System.out.println(d+"  "+Thread.currentThread().getName());

    }

    public static void main(String[] args) {
//        MyThread myThread  = new MyThread();
        for (int i = 0; i < 10; i++) {
            new MyThread().start();

        }

    }
}

2020-01-23 21:15:54 Thread-1
2020-01-23 21:15:54 Thread-8
2020-01-23 21:15:54 Thread-7
2020-01-23 21:15:54 Thread-5
2020-01-23 21:15:54 Thread-4
2020-01-23 21:15:54 Thread-3
2020-01-23 21:15:54 Thread-0
2020-01-23 21:15:54 Thread-2
2020-01-23 21:15:54 Thread-6
2020-01-23 21:15:54 Thread-9

 

2.Runnable :

class MyRunnable implements Runnable {

    int i =0;
    @Override
    public  void run(){
        System.out.println(Thread.currentThread().getName()+"  "+ i++);
    }


    public static void main(String[] args) {
        Runnable  implRunnable = new MyRunnable();
//        Thread thread = new Thread(implRunnable);
        for (int i = 1; i <5 ; i++) {
//            new Thread(implRunnable).start();  // int i全局线程操作共享
            new Thread(new MyRunnable()).start();  //  int  i 变量线程操作独立,
        }

    }
}

  

3. Callable:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable<Object> {

    @Override
    public Object call() throws Exception{
        int  result =0 ;
        for (int j = 0; j <8 ; j++) {
            result +=j;
        }
        System.out.println(Thread.currentThread().getName());
       return result;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        for (int i = 0; i <5 ; i++) {
            Callable<Object> callable  = new MyCallable() ;
            FutureTask<Object> futureTask = new FutureTask<Object>(callable);
           new Thread(futureTask).start();
           System.out.println(futureTask.get());

        }

    }
    
}

  

Thread-0
28
Thread-1
28
Thread-2
28
Thread-3
28
Thread-4
28

4.ThreadPool使用同步原语,重入锁,同步代码块,代码类,或者对象,对基本数据类型无效:

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

public class MyExecuter implements  Runnable{

    private  int i=0;
    @Override
    public  void run(){
        while (i<10) {

            System.out.println( Thread.currentThread().getName());
            i++;
        }
    }

    public static void main(String[] args) {
        ExecutorService  pool   = Executors.newFixedThreadPool(5);
        for (int i = 0; i <10; i++) {
            pool.submit(new MyExecuter());
        }

        pool.shutdown();  //shutdown all task  wait all task finish ,not longer recv  new task ,shutdownNow 立即关闭线程池
    }
}

  

以上是关于java 多线程实现四种方式解析Thread,Runnable,Callable,ServiceExcutor,Synchronized ,ReentrantLock的主要内容,如果未能解决你的问题,请参考以下文章

Java多线程的四种实现方式

Java实现多线程的四种方式

JAVA多线程实现的四种方式

JAVA中实现多线程的四种方式

Java多线程实现的四种方式

实现多线程的四种方式