如何在Java 9的JShell中创建线程?
JShell 是Java9中引入的交互式JavaShell工具,它使我们能够执行代码段,并立即显示结果,而无需像Java那样声明main()方法。它是一个REPL(读取-评估-打印循环)工具,从命令行提示符下运行。我们可以使用JShell创建变量,方法,类,暂存变量,外部库等。
在下面的代码片段中,我们可以通过扩展Thread类来创建线程。
C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro
jshell> class ThreadTest extends Thread {
...> public void run() {
...> System.out.println("Thread in run() method");
...> }
...> public static void main(String args[]) {
...> ThreadTest t = new ThreadTest();
...> t.start();
...> }
...> }
| created class ThreadTest在下面的代码片段中,控制台将向用户输出“方法中的线程run()”作为输出。
jshell> new ThreadTest().run(); Thread in run() method
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短