Thread.sleep 和Object.join的使用

sleep:Thread的静态方法,当前线程休眠一段时间,时间到了再恢复可运行状态,时间到了不一定就执行吧,还得竞争CPU呢.

join:这个方法其实就是特殊的wait,wait方法一般都需要别人notify(当然也可以设置超时),但是join方法就不需要别人notify了,一直等到这个线程死亡(就相当于这个线程临时前告诉那些在等它的人:你们进来吧!)

本人不是很会举例子,还是两个人公用一个卫生间吧,这回不刷牙了,改洗澡吧,总不能两个人同时洗澡吧!就算可以,这里假设不可以吧.情况时这样的:A在洗澡,B要等。

第一种情况:
B很聪明的,A洗澡可能要20分钟到1小时,我就先睡10分钟看看好了没有,没有好就再睡10分钟,最多多等10分钟而已吧.

class Syn
{
        public static void main(String[] args) throws Exception
       {
               Thread a=new Bathing();
                a.start();
                //B
                int time=0;
                while(a.isAlive()){
                        Thread.sleep(10000);
                        time+=10;
                        System.out.println("B has waited "+time+" minutes");
                }
                System.out.println("B can bath now!");
        }
}

class Bathing extends Thread
{
        public void run(){
                bathing();
        }
        private void bathing() {
                System.out.println("A is bathing !");
                try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
                //延迟20秒看效果
                System.out.println("A has bathed !");
        }
};

这里连同步都不需要,不过B可能需要多等一段时间,因为它可能刚好去敲门A还没有洗完,于是他又去睡,结果刚睡下A就洗完了.
第二种情况:
B变得更加聪明了,这样等我不是亏了,如果我10分钟敲一次门,我可能要多等10分钟,但是如果我每秒敲一次我也没法睡呀,于是想了一个高招,装了一个机关,当A出来的时候机关就会按响门铃,这样B就可以高枕无忧了。

class Syn
{
        public static void main(String[] args) throws Exception
        {
                Thread a=new Bathing();
                a.start();
                //B
                int time=0;
                a.join();
                System.out.println("B can bath now!");
          }
}

class Bathing extends Thread
{
        public void run(){
                bathing();
        }
        private void bathing() {
                System.out.println("A is bathing !");
                try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
                //延迟20秒看效果
                System.out.println("A has bathed !");
        }
};

这样只要A一洗完,B就会被唤醒,这里A并没有去notify他,但是还是间接的通知了B,当然这里也可以用wati和notify实现,不过就显得不好了。

class Syn
{
        public static void main(String[] args) throws Exception
        {
                Thread a=new Bathing();
                a.start();
                //B
                int time=0;
                synchronized(a){a.wait();}
                System.out.println("B can bath now!");
        }
}

class Bathing extends Thread
{
        public void run(){
                synchronized(this){
                    bathing();
                    notify();
                }
        }
        private void bathing() {
                System.out.println("A is bathing !");
                try{Thread.sleep(20000);}catch(InterruptedException e){e.printStackTrace();}
                //延迟20秒看效果
                System.out.println("A has bathed !");
        }
};

对于一般的对象就要用wait和notify了,但是对于一个线程来说,join方法有时候更加方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果需要在 thread1 和 thread2 中创建多个线程,可以在 setDatagridview11 和 setDatagridview22 方法中使用 Thread 类来创建新线程。然后使用 Start 方法来启动新线程,并使用 Join 方法等待它们完成。 下面是一个示例代码: ``` using System; using System.Threading; class Program { static void Main(string[] args) { Console.WriteLine("Main thread started."); Thread thread1 = new Thread(new ThreadStart(setDatagridview11)); Thread thread2 = new Thread(new ThreadStart(setDatagridview22)); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); Console.WriteLine("Main thread finished."); Console.ReadKey(); } static void setDatagridview11() { Console.WriteLine("setDatagridview11 started."); for (int i = 0; i < 5; i++) { Thread childThread = new Thread(new ParameterizedThreadStart(ChildThreadFunc)); childThread.Start(i); } Console.WriteLine("setDatagridview11 finished."); } static void setDatagridview22() { Console.WriteLine("setDatagridview22 started."); for (int i = 0; i < 5; i++) { Thread childThread = new Thread(new ParameterizedThreadStart(ChildThreadFunc)); childThread.Start(i + 5); } Console.WriteLine("setDatagridview22 finished."); } static void ChildThreadFunc(object index) { Console.WriteLine("Child thread {0} started.", index); Thread.Sleep(1000); Console.WriteLine("Child thread {0} finished.", index); } } ``` 在这个示例中,setDatagridview11 和 setDatagridview22 方法分别创建了 5 个新线程,并在其中调用了 ChildThreadFunc 方法。在 Main 方法中,启动 thread1 和 thread2 线程,并使用 Join 方法等待它们完成。运行程序后,你将会看到如下输出: ``` Main thread started. setDatagridview11 started. setDatagridview22 started. Child thread 0 started. Child thread 5 started. Child thread 1 started. Child thread 6 started. Child thread 2 started. Child thread 7 started. Child thread 3 started. Child thread 8 started. Child thread 4 started. Child thread 9 started. Child thread 0 finished. Child thread 5 finished. Child thread 1 finished. Child thread 6 finished. Child thread 2 finished. Child thread 7 finished. Child thread 3 finished. Child thread 8 finished. Child thread 4 finished. Child thread 9 finished. setDatagridview11 finished. setDatagridview22 finished. Main thread finished. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值