概览

前段时间有同事提到了主线程这个名词,但当时我们说的主线程是指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. centos7下PHP安装gd扩展

    第一步: 安装需要用到的库 yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel 第二步: ...

  2. VirtualBox 安装Ubuntu(16.04/18.04)时显示不全的解决方法

    是是系统分辨率不同导致的问题 Alt+鼠标左键 (16.04版本亲测有效,18.04版本亲测无效)或者Win+鼠标左键 (18.04版本亲测有效)拖动安装界面,即可显示内容.

  3. 一篇搞定Java集合类原理

    Java集合类实现原理 1.Iterable接口 定义了迭代集合的迭代方法 iterator() forEach() 对1.8的Lambda表达式提供了支持 2. Collection接口 定义了集合 ...

  4. ts流中的pcr与pts计算与逆运算

    mpeg2ts文件格式中有pcr和pts的概念,其代码含义如下: PCR(Program Clock Reference)--指示系统时钟本身的瞬时值的时间标签称为节目参考时钟标签(PCR). PTS ...

  5. 一些bug

    长期留坑 1.AC自动机多模式串匹配问题 对于要统计各个模式串在文本中的出现次数,对于每个当前节点不能直接暴力跳$fail$ 复杂可以退化到$O(n^2)$ $aaaaaa--aaa$可以卡掉 要将$ ...

  6. Java_进程与线程

    进Process&Thread 区别 进程 线程 根本区别 作为资源分配的单位 调度和执行的单位 开销 每个进程都有独立的代码和数据空间(进程上下文), 进程间的切换会有较大的开销 线程可以看 ...

  7. The Product-Minded Software Engineer

    转自The Product-Minded Software Engineer Product-minded engineers are developers with lots of interest ...

  8. jquery播放图片

    * { margin:0; padding:0; word-break:break-all; } body { background:#FFF; color:#333; font:12px/1.5em ...

  9. Java iText+FreeMarker生成PDF(HTML转PDF)

    1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等.方便用户查看,下载,打印.目前常用的解决方案是,把相关数据信息,生成对应的pdf文件返回给用户. ...

  10. Redis缓存穿透和雪崩

    缓存穿透 用户想要查询一个数据 在redis缓存数据库中没有获取到 就会向后端的数据库中查询. 当用户很多 都去访问后端数据库的话,这就会给数据库带来很大的压力. 常见场景:秒杀活动 等 解决方法: ...