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