概览

前段时间有同事提到了主线程这个名词,但当时我们说的主线程是指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中的主线程的更多相关文章

  1. Java中如何创建线程

    Java中如何创建线程 两种方式:1)继承Thread类:2)实现Runnable接口. 1.继承Thread类 继承Thread类,重写run方法,在run方法中定义需要执行的任务. class M ...

  2. Java中的守护线程 & 非守护线程(简介)

    Java中的守护线程 & 非守护线程 守护线程 (Daemon Thread) 非守护线程,又称用户线程(User Thread) 用个比较通俗的比如,任何一个守护线程都是整个JVM中所有非守 ...

  3. ThreadLocal,Java中特殊的线程绑定机制

    在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...

  4. [置顶] 使用严苛模式打破Android4.0以上平台应用中UI主线程的“独断专行”

    传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 已经有好一段时间没有关注Android应用方面的事情了:)最近单位来了一个Androi ...

  5. (转)Java中的守护线程

    Java的守护线程与非守护线程   守护线程与非守护线程 最近在看多线程的Timer章节,发现运用到了守护线程,感觉Java的基础知识还是需要补充. Java分为两种线程:用户线程和守护线程 所谓守护 ...

  6. Java-ThreadLocal,Java中特殊的线程绑定机制

    在DRP项目中,我们使用了ThreadLocal来创建Connection连接,避免了一直以参数的形式将Connection向下传递(传递connection的目的是由于jdbc事务要求确保使用同一个 ...

  7. Java 中如何实现线程间通信

    世界以痛吻我,要我报之以歌 -- 泰戈尔<飞鸟集> 虽然通常每个子线程只需要完成自己的任务,但是有时我们希望多个线程一起工作来完成一个任务,这就涉及到线程间通信. 关于线程间通信本文涉及到 ...

  8. 关于Java中进程和线程的详解

    一.进程:是程序的一次动态执行,它对应着从代码加载,执行至执行完毕的一个完整的过程,是一个动态的实体,它有自己的生命 周期.它因创建而产生,因调度而运行,因等待资源或事件而被处于等待状态,因完成任务而 ...

  9. Java中怎样创建线程安全的方法

    面试问题: 下面的方法是否线程安全?怎样让它成为线程安全的方法? class MyCounter { private static int counter = 0; public static int ...

随机推荐

  1. eclipse安装报错

    例如这样 原因是被墙了 个人搭**后完美解决

  2. Vue.js 获得兄弟元素,子元素,父元素(DOM操作)

    e.target 是你当前点击的元素 e.currentTarget 是你绑定事件的元素 e.currentTarget.previousElementSibling.innerHTML 获得点击元素 ...

  3. 【DeepLearning】GoogLeNet

    InceptionV1 论文原文:Going deeper with convolutions    中英文对照 InceptionBN 论文原文:Batch Normalization: Accel ...

  4. 简单粗暴套娃模式组json发送https请求

    各位童鞋大家好,向来简单粗暴的铁柱兄给大家来玩一手套娃模式来组Json数据,不说别的,无脑套. 当然,这一手比较适合临场用一下,若长期用的话建议搞一套适用的框架,只管set就好了.话不多说开始上课. ...

  5. python机器学习实现逻辑斯蒂回归

    逻辑斯蒂回归 关注公众号"轻松学编程"了解更多. [关键词]Logistics函数,最大似然估计,梯度下降法 1.Logistics回归的原理 利用Logistics回归进行分类的 ...

  6. python中的 异常处理(try...expect)

    错误处理 关注公众号"轻松学编程"了解更多. 在程序运行的过程中,如果发生了错误,可以事先约定一个错误代码,这样就可以知道是否有错,以及出错的原因,在操作系统的调用中,返回错误码的 ...

  7. 【CHOJ】磁力块

    题意描述 磁力块 在平面内分布着 \(N\) 个磁力块,同时你的手上也有一块. 你一开始站在给定的坐标上,当磁力块之间满足互相吸引的条件时就可以吸引. 当你拿到新的磁石时你就可以用它来吸引更多的石头, ...

  8. (2)ASP.NET Core3.1 Ocelot路由

    1.路由 前一个章节我们已经介绍过Ocelot,相信大家也了解到,Ocelot的主要功能是接收客户端等传入的HTTP请求,并将其转发到下游服务.Ocelot当前仅以另一个http请求的形式支持此功能( ...

  9. Linux 环境下 C++ 的开发编译

    Linux环境下C++程序的开发编译学习笔记 环境:vmware 运行下的Ubuntu 16.04 姓名:谢津 时间:2018/5/24 内容:1)vim的安装及配置:2)第一个C++程序的编写与编译 ...

  10. 【原创】Linux虚拟化KVM-Qemu分析(五)之内存虚拟化

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...