java多线程入门知识及示例程序
时间:2021-11-30 03:17:58
为什么需要多线程?
模型的简化,如某些程序是由多个相对独立任务的运行:
图形界面的出现,输入、输出的阻塞
多核CPU的更好利用
异步行为的需要
Java多线程的特性:
程序的入口main本身是一个线程
线程是并发的,无序执行的
线程内部是顺序执行的
共享数据
Java多线程的风险:
安全风险:由于线程的操作顺序是不确定的,某些在单线程下能运行的程序到多线程下会出现意外的结果。
性能风险:服务器的吞吐量、响应性、资源消耗
Java多线程API:
Java可以通过两种形式创建线程:一、实现Runnable接口,二、继承Thread类。
继承thread创建线程示例代码
public class ThreadTest extends Thread {
public static void main(String[] args) {
ThreadTest thread = new ThreadTest();
thread.start();
for (int i=0; i<10; i++) {
System.out.println("main:"+i);
}
}
@Override
public void run() {
for (int i=0; i<10; i++) {
System.out.println("thread:"+i);
}
}
}
实现runnable创建线程代码
package com.openrdp.thread.api;
public class RunnableTest implements Runnable {
public static void main(String[] args) {
RunnableTest runnable = new RunnableTest();
Thread thread = new Thread(runnable);
thread.start();
for (int i=0; i<10; i++) {
System.out.println("main:"+i);
}
}
@Override
public void run() {
for (int i=0; i<10; i++) {
System.out.println("thread:"+i);
}
}
}
Java线程池技术
Executors获取exceuctorservice线程池代码
package com.openrdp.thread.api;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TreadPoolTest {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(99);
TaskThread thread1 = new TaskThread("t1");
threadPool.execute(thread1);
TaskThread thread2 = new TaskThread("t2");
threadPool.execute(thread2);
}
static class TaskThread implements Runnable {
String param;
public TaskThread(String param) {
this.param = param;
}
@Override
public void run() {
}
}
}
标签:java多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Flyweight模式应用实践的相关介绍
2021-07-28 21:50:34
Java实现LeetCode(54.螺旋矩阵)
2023-01-26 20:54:00
Spring MVC全局异常实例详解
2021-11-19 13:51:26
![](https://img.aspxhome.com/file/2023/7/66477_0s.png)
详解OpenCV For Java环境搭建与功能演示
2023-05-27 09:13:50
![](https://img.aspxhome.com/file/2023/8/81358_0s.jpg)
C#双缓冲实现方法(可防止闪屏)
2021-07-07 18:15:33
JavaWeb文件上传入门教程
2022-04-22 10:11:46
深入浅出讲解Java集合之Collection接口
2023-05-27 05:53:38
![](https://img.aspxhome.com/file/2023/2/89622_0s.png)
JAVA使用POI获取Excel的列数与行数
2021-10-03 12:53:37
SpringCloud Eureka的使用教程
2022-03-23 22:30:59
![](https://img.aspxhome.com/file/2023/8/70348_0s.png)
JVM之方法返回地址详解
2022-05-02 07:58:27
![](https://img.aspxhome.com/file/2023/0/88590_0s.png)
java获取ip地址示例
2021-12-25 07:04:22
java实现简单猜数字
2022-12-31 16:20:02
![](https://img.aspxhome.com/file/2023/0/73060_0s.png)
springboot+jwt+微信小程序授权登录获取token的方法实例
2022-07-11 17:53:57
![](https://img.aspxhome.com/file/2023/5/92725_0s.jpg)
举例解析Java的图像缓冲技术的使用
2022-09-08 17:17:17
一款域名监控小工具 Domain(IP)Watcher 实现代码
2023-09-15 11:55:44
![](https://img.aspxhome.com/file/2023/6/98256_0s.jpg)
RegexOptions.IgnoreCase正则表达式替换,忽略大小写
2022-05-03 17:25:45
Java微信公众平台开发(13) 微信JSSDK中Config配置
2022-12-29 15:58:24
![](https://img.aspxhome.com/file/2023/9/67439_0s.jpg)
Java class文件格式之属性_动力节点Java学院整理
2021-06-13 01:52:00
![](https://img.aspxhome.com/file/2023/4/101944_0s.png)
解析Java的Hibernate框架中的持久化类和映射文件
2023-08-19 15:00:05
hadoop运行java程序(jar包)并运行时动态指定参数
2023-07-27 11:02:10
![](https://img.aspxhome.com/file/2023/9/57829_0s.png)