Thread.join和Synchronized有什么区别? [英] What is the difference between Thread.join and Synchronized?

查看:178
本文介绍了Thread.join和Synchronized有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑何时使用 Thread.join()以及何时在多线程应用程序中使用同步

I am confused when to use Thread.join() and when to use synchronization in multi threading application.

据我所知,他们都阻止或等待其他线程执行。

这个例子必须输出10个A,10个B&一个接一个的顺序模式10 C,如:

According to me, both of them block or wait for the execution to be done by some other thread.
This example has to output 10 A's , 10 B's & 10 C's in sequential pattern one after other like :

1  : A
2  : A
3  : A
4  : A
5  : A
6  : A
7  : A
8  : A
9  : A
10 : A
1  : B
2  : B
3  : B
4  : B
5  : B
6  : B
7  : B
8  : B
9  : B
10 : B
1  : C
2  : C
3  : C
4  : C
5  : C
6  : C
7  : C
8  : C
9  : C
10 : C
----ProGraM ENDS----

示例从这里开始

class SyncTest extends Thread 
{   
    StringBuffer sb;

    public SyncTest(StringBuffer sb) 
    {
        this.sb = sb;   
    }

    public void run()
    {
        synchronized(sb) 
        {
            for(int i=1;i<=10;i++){
                System.out.println(i+" : "+sb.charAt(0));
            }
            sb.setCharAt(0, (char) (sb.charAt(0)+1));
        }
    }

    public static void main(String [] args) throws InterruptedException
    {
        StringBuffer sb = new StringBuffer("A");
        Thread t1=new SyncTest(sb);
        Thread t2=new SyncTest(sb);
        Thread t3=new SyncTest(sb);

        t1.start();

        t2.start();

        t3.start();

        Thread.sleep(1000);

        System.out.println("----ProGraM ENDS----");
    }
}

这里输出结果是10 A然后是按顺序排列10 B,然后是10 C。但我也可以使用 Thread.join 而不是 synchronized 块来获得相同的输出:

Here, output turns out to be 10 A's followed by 10 B's followed by 10 C's in a sequential order. But I can also use Thread.join instead of synchronized block to get the same output like this:

public void run()
    {
        //removed synchronized statement...

            for(int i=1;i<=10;i++){
                System.out.println(i+" : "+sb.charAt(0));
            }
            sb.setCharAt(0, (char) (sb.charAt(0)+1));

    }

    public static void main(String [] args) throws InterruptedException
    {
        StringBuffer sb = new StringBuffer("A");
        Thread t1=new SyncTest(sb);
        Thread t2=new SyncTest(sb);
        Thread t3=new SyncTest(sb);

        t1.start();
        t1.join();
        t2.start(); // wait for t1 to complete
        t2.join();
        t3.start(); // wait for t2 to complete
        t3.join(); 

                     // wait for t3 to complete
        System.out.println("----ProGraM ENDS----");
    }

任何人都可以清楚我对使用这两种技术的困惑,即何时使用 Thread.join 以及何时在Java上的多线程中使用同步

Can anyone clear my confusion on usage of these 2 techniques i.e. when to use Thread.join and when to use synchronization in Multi-threading on Java.

推荐答案

Thread.join()等待线程完全完成,而同步 block可用于防止两个线程同时执行同一段代码。

Thread.join() waits for the thread to completely finish, whereas a synchronized block can be used to prevent two threads from executing the same piece of code at the same time.

一般来说,很难建议何时使用其中一种,因为它们用于不同的目的。很难找到一个例子,例如你的代码,两者之间的差异很小。

It's hard to advise when to use one over the other in general, since they serve different purposes. It's rare to find an example, such as your code, where the difference between the two is minimal.

这就是说,在你的第一个例子中,不能保证输出将按字母顺序排列。您无法确定哪个线程将首先进入 synchronized 块。所以在这种特殊情况下, join()是最合适的。

That being said, in your first example there is no guarantee the output will be alphabetical. You can't be sure which thread will get to the synchronized block first. So in this particular case, join() is most appropriate.

这篇关于Thread.join和Synchronized有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆