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); }
}

  

最新文章

  1. 静态属性,直接把iis搞垮掉 Http error 503 Service Unavailable
  2. Ubuntu 14.10 下进程实时IO监控iotop命令详解
  3. sourceforge软件下载方式
  4. rebar
  5. jQuery如何实现点击页面获得当前点击元素
  6. vue端口号被占用如何解决
  7. windows server 几大实时同步软件比较
  8. vmware workstation安装教程以及其中出现的错误解决方法
  9. 物化视图(materialized view) 实现数据迁移、数据定时同步
  10. Python-Unittest
  11. Linux上安装jdk,mysql
  12. JAVA RSA加密AES加密
  13. tensorflow 调试tfdbg
  14. asp.net core 如何集成kindeditor并实现图片上传功能
  15. Swift 重点知识汇总
  16. sitecore系统教程之内容编辑器
  17. React/anu实现弹出层2
  18. hdu 5194 组合数学or暴力
  19. ESB初步配置文件认识
  20. iOS 枚举讲解

热门文章

  1. laravel手动数组分页
  2. jrtp 使用
  3. Linux第一周作业
  4. unity目前学的一些操作
  5. 堆排序用JavaScript实现
  6. 如何通过 SSH/Telnet 用 root 权限登录群晖
  7. [P5665][CSP2019D2T2] 划分
  8. c#判断字符串是否可以转日期格式
  9. LeetCode 1046. 最后一块石头的重量 (贪心)
  10. pod has unbound immediate PersistentVolumeClaims : statefulset挂载不上pv的另一种情况