下列代码执行结果为(A) public static void main(String args[])throws InterruptedException{             Thread t=new Thread(new Runnable() {                 public void run() {                     try {                         Thread.sleep(2000);                     } catch (InterruptedException e) {                         throw new RuntimeException(e);                     }                     System.out.print("2");                 }             });             t.start();                           t.join();             System.out.print("1");         }

区块链毕设网qklbishe.com为您提供问题的解答

下列代码执行结果为(A)

public static void main(String args[])throws InterruptedException{

            Thread t=new Thread(new Runnable() {

                public void run() {

                    try {

                        Thread.sleep(2000);

                    } catch (InterruptedException e) {

                        throw new RuntimeException(e);

                    }

                    System.out.print("2");

                }

            });

            t.start();

             

            t.join();

            System.out.print("1");

        }

start: 启动线程,实现了多线程运行,无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。 

join:很多情况下,主线程生成并起动了子线程,如果子线程里要进行大量的耗时的运算,主线程往往将于子线程之前结束,但是如果主线程处理完其他的事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后再结束,这个时候就要用到join()方法了。
所以System.out.print(21);

编辑于 2022-05-18 21:11:14
join的作用是先执行了子线程t后,在执行主线程
30:16

以上就是关于问题下列代码执行结果为(A) public static void main(String args[])throws InterruptedException{             Thread t=new Thread(new Runnable() {                 public void run() {                     try {                         Thread.sleep(2000);                     } catch (InterruptedException e) {                         throw new RuntimeException(e);                     }                     System.out.print("2");                 }             });             t.start();                           t.join();             System.out.print("1");         }的答案

欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。

区块链NFT链游项目方科学家脚本开发培训

承接区块链项目定制开发

微信:btc9767

QQ :1330797917

TELEGRAM: BTCOK9

承接区块链项目定制开发


qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台-javagopython毕设 » 下列代码执行结果为(A) public static void main(String args[])throws InterruptedException{             Thread t=new Thread(new Runnable() {                 public void run() {                     try {                         Thread.sleep(2000);                     } catch (InterruptedException e) {                         throw new RuntimeException(e);                     }                     System.out.print("2");                 }             });             t.start();                           t.join();             System.out.print("1");         }