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

1.Thread实现:

1import java.util.Date; 2import java.text.SimpleDateFormat; 3 4public class MyThread extends Thread{ 5 @Override 6 public void run(){ 7 8 SimpleDateFormat strf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 9 String d = strf.format(new Date());// new Date()为获取当前系统时间 10 System.out.println(d+" "+Thread.currentThread().getName()); 11 12 } 13 14 public static void main(String[] args) { 15// MyThread myThread = new MyThread(); 16 for (int i = 0; i < 10; i++) { 17 new MyThread().start(); 18 19 } 20 21 } 22}

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 :

1class MyRunnable implements Runnable { 2 3 int i =0; 4 @Override 5 public void run(){ 6 System.out.println(Thread.currentThread().getName()+" "+ i++); 7 } 8 9 10 public static void main(String[] args) { 11 Runnable implRunnable = new MyRunnable(); 12// Thread thread = new Thread(implRunnable); 13 for (int i = 1; i <5 ; i++) { 14// new Thread(implRunnable).start(); // int i全局线程操作共享 15 new Thread(new MyRunnable()).start(); // int i 变量线程操作独立, 16 } 17 18 } 19}

3. Callable:

1import java.util.concurrent.Callable; 2import java.util.concurrent.ExecutionException; 3import java.util.concurrent.FutureTask; 4 5public class MyCallable implements Callable<Object> { 6 7 @Override 8 public Object call() throws Exception{ 9 int result =0 ; 10 for (int j = 0; j <8 ; j++) { 11 result +=j; 12 } 13 System.out.println(Thread.currentThread().getName()); 14 return result; 15 } 16 17 public static void main(String[] args) throws InterruptedException, ExecutionException { 18 for (int i = 0; i <5 ; i++) { 19 Callable<Object> callable = new MyCallable() ; 20 FutureTask<Object> futureTask = new FutureTask<Object>(callable); 21 new Thread(futureTask).start(); 22 System.out.println(futureTask.get()); 23 24 } 25 26 } 27 28}

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

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

1import java.util.concurrent.locks.ReentrantLock; 2 3public class SynchronizedTest implements Runnable { 4 private int i =10; 5 // synchronized mean block one thread opt some var util other th have fish some operation ; 6 @Override 7 public void run() { 8 //或者同步代码块 Integer Object ,int是基本数据类型,不是object; 9 //synchronized(SychronizeTest.class) { 10 ReentrantLock RLock = new ReentrantLock(); 11 RLock.lock(); 12 if (i >= 1) { 13 14 try { 15 Thread.sleep(100); 16 i--; 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 System.out.println(Thread.currentThread().getName() + " " + i); 21 } else { 22 System.out.println("库存不足"); 23 } 24 RLock.unlock(); 25 //} 26 } 27 public static void main(String[] args) throws Exception{ 28 Runnable runnable = new SynchronizedTest(); 29 for (int i = 0; i < 20; i++) { 30 new Thread(runnable).start(); 31 32 } 33 Thread.sleep(3000); 34 35 } 36}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java多线程实现的三种方式

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。1、继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现

java多线程和异步回调

   在实际开发过程中遇到的多线程情况不多,但是在生产环境中多线程是最基本的情况,java面试时也会考到,所以看看多线程的知识还是很有必要的。 Thread,Runnable,Callable,Future,FutureTask,Executors这是java常见的接口和类。  thread.run():线程具体要执行的代码,thread.jo

一篇文章弄懂Java多线程基础和Java内存模型

文章目录一、多线程的生命周期及五种基本状态二、Java多线程的创建及启动1.继承Thread类,重写该类的run()方法2.通过实现Runnable接口创建线程类3.通过Callable和Future接口创建线程三、Java内存模型概念四、内存间的交互操作五、volatile和synchronized的

Java面试系列

实现多线程的方式继承Thread类,重写run方法,调用start方法启动线程实现Runnable接口,重写run方法,调用start方法启动线程实现Callable接口,重写call方法,并用FutureTask包装,在newThread中传入FutureTask,然后调用start方

Java多线程基础

(1)传统使用类Thread和接口Runnable实现 1\.在Thread子类覆盖的run方法中编写运行代码方式一 newThread(){@Overridepublicvoidrun(){while(true){try{Thread.sleep(2

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