码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-23 22:32:19      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:get   except   vat   imp   executors   read   util   call()   关闭   

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

标签:get   except   vat   imp   executors   read   util   call()   关闭   

原文地址:https://www.cnblogs.com/SunshineKimi/p/12231414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!