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.locks.ReentrantLock;

public class SynchronizedTest implements Runnable {
    private  int i =10;
    // synchronized   mean  block one   thread opt some var  util other th have fish some operation ;
    @Override
    public   void run() {
        //或者同步代码块 Integer Object  ,int是基本数据类型,不是object;
        //synchronized(SychronizeTest.class) {
        ReentrantLock  RLock = new ReentrantLock();
        RLock.lock();
            if (i >= 1) {

                try {
                    Thread.sleep(100);
                    i--;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "  " + i);
            } else {
                System.out.println("库存不足");
            }
         RLock.unlock();
        //}
    }
    public static void main(String[] args)  throws Exception{
        Runnable runnable  =  new SynchronizedTest();
        for (int i = 0; i < 20; i++) {
            new Thread(runnable).start();

        }
        Thread.sleep(3000);

    }
}

  

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