Java中的主线程
概览
前段时间有同事提到了主线程这个名词,但当时我们说的主线程是指Java Web程序中每一个请求进来时处理逻辑的线程。当时感觉这个描述很奇怪,所以就来研究下这个主线程的确切语义。
Java提供了内置的多线程编程支持,多线程包括两个或多个可并发执行的部分,每一部分叫做线程,每个线程定义了单独的执行部分。
主线程
当一个Java程序启动的时候,会有一个线程立即开始运行,这个线程通常被我们叫做程序中的主线程,因为它是在我们程序开始的时候就被执行的线程。
- 子线程都从该线程中被孵化
- 通常它都是最后一个执行结束的线程,因为它会执行各种的关闭操作。
流程图:

怎么来控制主线程
主线程在程序启动时会被自动创建,为了能够控制它我们必须获取到它的引用,我们可以在当前类中调用currentThread()方法来获取到,该方法返回一个当前线程的引用。
主线程默认的优先级是5,所有子线程都将继承它的优先级。
public class Test extends Thread {
public static void main(String[] args) {
// getting reference to Main thread
Thread t = Thread.currentThread();
// getting name of Main thread
System.out.println("Current thread: " + t.getName());
// changing the name of Main thread
t.setName("Geeks");
System.out.println("After name change: " + t.getName());
// getting priority of Main thread
System.out.println("Main thread priority: " + t.getPriority());
// setting priority of Main thread to MAX(10)
t.setPriority(MAX_PRIORITY);
System.out.println("Main thread new priority: " + t.getPriority());
for (int i = 0; i < 5; i++) {
System.out.println("Main thread");
}
// Main thread creating a child thread
ChildThread ct = new ChildThread();
// getting priority of child thread
// which will be inherited from Main thread
// as it is created by Main thread
System.out.println("Child thread priority: " + ct.getPriority());
// setting priority of Main thread to MIN(1)
ct.setPriority(MIN_PRIORITY);
System.out.println("Child thread new priority: " + ct.getPriority());
// starting child thread
ct.start();
}
}
// Child Thread class
class ChildThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Child thread");
}
}
}
看下输出:
Current thread: main
After name change: Geeks
Main thread priority: 5
Main thread new priority: 10
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread priority: 10
Child thread new priority: 1
Child thread
Child thread
Child thread
Child thread
Child thread
主线程和main()函数的关系
对每个程序而言,主线程都是被JVM创建的,从JDK6开始,main()方法在Java程序中是强制使用的。
主线程中的死锁(单个线程)
我们可以使用主线程创建一个死锁:
public class Test {
public static void main(String[] args) {
try {
System.out.println("Entering into Deadlock");
Thread.currentThread().join();
// the following statement will never execute
System.out.println("This statement will never execute");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
看下输出:
Entering into Deadlock
语句Thread.currentThread().join()会告诉主线程一直等待这个线程(也就是等它自己)运行结束,所以我们就有了死锁。
关于Java中的死锁和活锁可以参考这篇文章
Java中的主线程的更多相关文章
- Java中如何创建线程
Java中如何创建线程 两种方式:1)继承Thread类:2)实现Runnable接口. 1.继承Thread类 继承Thread类,重写run方法,在run方法中定义需要执行的任务. class M ...
- Java中的守护线程 & 非守护线程(简介)
Java中的守护线程 & 非守护线程 守护线程 (Daemon Thread) 非守护线程,又称用户线程(User Thread) 用个比较通俗的比如,任何一个守护线程都是整个JVM中所有非守 ...
- ThreadLocal,Java中特殊的线程绑定机制
在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...
- [置顶] 使用严苛模式打破Android4.0以上平台应用中UI主线程的“独断专行”
传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 已经有好一段时间没有关注Android应用方面的事情了:)最近单位来了一个Androi ...
- (转)Java中的守护线程
Java的守护线程与非守护线程 守护线程与非守护线程 最近在看多线程的Timer章节,发现运用到了守护线程,感觉Java的基础知识还是需要补充. Java分为两种线程:用户线程和守护线程 所谓守护 ...
- Java-ThreadLocal,Java中特殊的线程绑定机制
在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...
- Java 中如何实现线程间通信
世界以痛吻我,要我报之以歌 -- 泰戈尔<飞鸟集> 虽然通常每个子线程只需要完成自己的任务,但是有时我们希望多个线程一起工作来完成一个任务,这就涉及到线程间通信. 关于线程间通信本文涉及到 ...
- 关于Java中进程和线程的详解
一.进程:是程序的一次动态执行,它对应着从代码加载,执行至执行完毕的一个完整的过程,是一个动态的实体,它有自己的生命 周期.它因创建而产生,因调度而运行,因等待资源或事件而被处于等待状态,因完成任务而 ...
- Java中怎样创建线程安全的方法
面试问题: 下面的方法是否线程安全?怎样让它成为线程安全的方法? class MyCounter { private static int counter = 0; public static int ...
随机推荐
- viewPager删除缓存fragment
fragment结合viewpager会缓存fragment在内存,除非退出程序,想要不退出程序情况下刷新fragment页面,就要删除缓存; public class MainActivity ex ...
- 数据恢复软件推荐-easyrecovery绿色破解版(附注册码)免费下载
easyrecovery破解版专注于PC端存储数据的抢救恢复,软件的整体界面风格和360杀毒有些许相似,没有看起来像牛皮藓的杂乱广告,只有六个功能按键,对应你所遇到的数据丢失状况级别,点击最为适合的功 ...
- Pytorch加载并可视化FashionMNIST指定层(Udacity)
加载并可视化FashionMNIST 在这个notebook中,我们要加载并查看 Fashion-MNIST 数据库中的图像. 任何分类问题的第一步,都是查看你正在使用的数据集.这样你可以了解有关图像 ...
- VMware Workstation Pro 虚拟机安装
1.简介 虚拟机指通过软件莫比的具体有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. 我们可以通过虚拟机软件,可以在一台物理计算机模拟出一台或多台虚拟的计算机,这些虚拟的计算机完全就像 ...
- Tarjan缩点入门
缩点 顾名思义,缩点就是把一个强连通分量缩成一个点 Tarjan 在dfs的过程中记录时间戳,若能够通过某个点返回已遍历的点,则可以缩点 inline void Tarjan(int x)// st栈 ...
- vue-cli中使用swiper
1.当前项目配置 cnpm install swiper vue-awesome-swiper --save 或指定版本下载 cnpm install swiper@5.4.5 vue-awesome ...
- STM32入门系列-STM32时钟系统,时钟使能配置函数
之前的推文中说到,当使用一个外设时,必须先使能它的时钟.怎么通过库函数使能时钟呢?如需了解寄存器配置时钟,可以参考<STM32F10x中文参考手册>"复位和时钟控制(RCC)&q ...
- C++代码雨
闲逛的时候发现了一个很好玩的程序 摘自:https://blog.csdn.net/u012837895/article/details/20849967#comments 效果如下 #include ...
- php中Standard中配置选项,在TargetFrameworks环境下如何输出库存
在.NET Standard/.NET Core技术出现之前,编写一个类库项目(暂且称为基础通用类库PA)且需要支持不同 .NET Framework 版本,那么可行的办法就是创建多个不同版本的项目( ...
- c#练习习题:while循环
2006年培养学员80000人,每年增长25%,请问按此增长速度,到哪一年培训学员人数将达到20万人? int count = 80000; int year = 2006; while (count ...