码迷,mamicode.com
首页 > 编程语言 > 详细

主线程中调用线程的start()

时间:2018-12-24 23:36:04      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:技术分享   主线程   时间   system   print   void   ack   style   image   

public class Quest implements Runnable
{
    int b = 100;

    public synchronized void m1() throws Exception
    {
        System.out.println("enter m1");
        b = 1000;
        Thread.sleep(3000);
        System.out.println("b="+b);
    }
    
    public void m2() throws Exception
    {
        System.out.println("enter m2");
        Thread.sleep(3000);
        b = 2000;
    }
    

    @Override
    public void run()
    {
            try
            {
                m1();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
    }
    
    public static void main(String[] args)
    {
        try
        {
            Quest quest = new Quest();
            // 线程从调用start()到run()执行需要一定的时间
            new Thread(quest).start();
            // 所以此处会先执行m2()
            quest.m2();
            System.out.println(quest.b);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

结果:

  每次先打印 m2

 技术分享图片

 

主线程中调用线程的start()

标签:技术分享   主线程   时间   system   print   void   ack   style   image   

原文地址:https://www.cnblogs.com/virgosnail/p/10171352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!