java 多线程实现四种方式解析Thread,Runnable,Callable,ServiceExcutor,Synchronized ,ReentrantLock
2024-09-04 19:12:09
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); }
}
最新文章
- 静态属性,直接把iis搞垮掉 Http error 503 Service Unavailable
- Ubuntu 14.10 下进程实时IO监控iotop命令详解
- sourceforge软件下载方式
- rebar
- jQuery如何实现点击页面获得当前点击元素
- vue端口号被占用如何解决
- windows server 几大实时同步软件比较
- vmware workstation安装教程以及其中出现的错误解决方法
- 物化视图(materialized view) 实现数据迁移、数据定时同步
- Python-Unittest
- Linux上安装jdk,mysql
- JAVA RSA加密AES加密
- tensorflow 调试tfdbg
- asp.net core 如何集成kindeditor并实现图片上传功能
- Swift 重点知识汇总
- sitecore系统教程之内容编辑器
- React/anu实现弹出层2
- hdu 5194 组合数学or暴力
- ESB初步配置文件认识
- iOS 枚举讲解