1、继承Thread类

public class MyThread extends Thread {
public MyThread() { }
public void run() {
for(int i=0;i<10;i++) {
System.out.println(Thread.currentThread()+":"+i);
}
}
public static void main(String[] args) {
MyThread mThread1=new MyThread();
MyThread mThread2=new MyThread();
MyThread myThread3=new MyThread();
mThread1.start();
mThread2.start();
myThread3.start();
}
}

2、实现Runnable接口

public class MyThread implements Runnable{
public static int count=20;
public void run() {
while(count>0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+count--);
}
}
public static void main(String[] args) {
MyThread Thread1=new MyThread();
Thread mThread1=new Thread(Thread1,"线程1");
Thread mThread2=new Thread(Thread1,"线程2");
Thread mThread3=new Thread(Thread1,"线程3");
mThread1.start();
mThread2.start();
myThread3.start();
}
}

3、实现Callable接口

package com.yanshu;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; /**
* @author :yangyuanyuan
* @description:TODO
* @date :2021/1/22 14:05
*/ class Mythread implements Callable<Integer>{ @Override
public Integer call() throws Exception {
System.out.println("COME IN"); return 1024;
}
}
public class CallableDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask = new FutureTask(new Mythread());
new Thread(futureTask,"A").start();
Integer integer = futureTask.get();
System.out.println(integer); }
}

4、使用线程池

package com.yanshu;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* @author :yangyuanyuan
* @description:TODO
* @date :2021/1/22 14:38
*/
public class ThreadPool {
public static void main(String[] args) { /*
*FixThreadPool(int n); 固定大小的线程池
* 使用于为了满足资源管理需求而需要限制当前线程数量的场合。使用于负载比较重的服务器。
* pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-2 0
pool-1-thread-2 1
pool-1-thread-3 0
pool-1-thread-3 1
pool-1-thread-4 0
pool-1-thread-4 1
pool-1-thread-5 0
pool-1-thread-5 1
*
* */
// ExecutorService ex=Executors.newFixedThreadPool(5);
// for(int i=0;i<5;i++) {
// ex.submit(new Runnable() {
//
// @Override
// public void run() {
// for(int j=0;j<2;j++) {
// System.out.println(Thread.currentThread().getName()+"\t"+j);
// }
//
// }
// });
// }
// ex.shutdown(); /*
SingleThreadPoolExecutor :单线程池
需要保证顺序执行各个任务的场景
*
* pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-1 0
pool-1-thread-1 1
* /
ExecutorService ex=Executors.newSingleThreadExecutor(); for(int i=0;i<5;i++) {
ex.submit(new Runnable() { @Override
public void run() {
for(int j=0;j<2;j++) {
System.out.println(Thread.currentThread().getName()+"\t"+j);
} }
});
}
ex.shutdown(); /*
* CashedThreadPool(); 缓存线程池
当提交任务速度高于线程池中任务处理速度时,缓存线程池会不断的创建线程
适用于提交短期的异步小程序,以及负载较轻的服务器
*
* pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-2 0
pool-1-thread-2 1
pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-1 0
pool-1-thread-1 1
pool-1-thread-3 0
pool-1-thread-3 1
* */
ExecutorService ex=Executors.newCachedThreadPool(); for(int i=0;i<5;i++) {
ex.submit(new Runnable() { @Override
public void run() {
for(int j=0;j<2;j++) {
System.out.println(Thread.currentThread().getName()+"\t"+j);
} }
});
}
ex.shutdown(); }
}

最新文章

  1. Jenkins 集成打包和上传 App Store 的冲突
  2. A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()
  3. Android代码故事第一回,平均间隔的按钮
  4. Angular常用功能
  5. Dynamics AX7 materials
  6. [Top-Down Approach]My First C/S Program [Python]
  7. UITableView 系列之自定义 UITableViewCell
  8. NGUI Scroll List
  9. linux桌面应用开发之折腾
  10. TAT,我的LCT转双旋了
  11. apache高负载性能调优
  12. OCP读书笔记(19) - 数据库空间管理
  13. kafka第四篇--快速入门(如何使用kafka)
  14. jdbc连接oracle时报错 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableC
  15. python 获取随机字母
  16. 【杂谈】tocmat是何时写回响应数据报的
  17. 安全系列之CSRF初探
  18. sql server: quering roles, schemas, users,logins
  19. Hadoop生态圈-zookeeper的API用法详解
  20. ribbion的负载均衡之端口的切换

热门文章

  1. centos 8.x系统配置chrony时间同步服务
  2. 测试如何区分前后端bug
  3. 多媒体开发(5)&amp;音频特征:声音可以调大一点吗?
  4. Sentinel限流之快速失败和漏桶算法
  5. 【C++】《C++ Primer 》第九章
  6. 剑指offer-查找数组中重复的数字
  7. python学习笔记 | selenium各浏览器驱动下载地址
  8. 创建一个简单MyBatis程序
  9. MySQL全面瓦解20:可编程性之流程控制语句
  10. Python入门之修改jupyter启动目录