0

I was given a question in class saying 'What are the possible final values of a? (Assume that each statement is a unit of execution. You do not need to consider the problem at the instruction level.)'. With additional information:

Thread A: a = 3 (A1) and a = a + 1 (A2)

Thread B: a = 5 (B1) and a = a + 7 (B2)

So after some thought that First thread output should be 4 and second threads output should be 12.

So I did the test script to see if that's correct and output shows what I expected Thread A: 4 and Thread B: 12.

The question is, should I expect other values? Or maybe I'm just implementing this question wrong? How I can tweak this code to get other values, if possible? Is that a tricky question, or its just me?

EDIT: Since code was not really needed for my homework assignment, lets focus on theoretical implementation of such problem.

4

5 に答える 5

2

これを修正して、順次実行ではなく実際にスレッド化を示す場合、正しい答えは、同期がない場合の動作は未定義であるということです。

于 2012-09-17T02:58:31.103 に答える
2

a現在の CPU 速度とスレッド/スケジューリングの開始のオーバーヘッドを考えると、コードを修正してスレッドを正しく実装すると仮定すると、同時実行のバグは見られないでしょう。他の 1 つは開始する可能性があるため、おそらく常に結果として 4 と 12 が得られます。

データ競合が発生するには、はるかに長いタスクが必要になります。

于 2012-09-17T05:53:47.560 に答える
1

FirstThreadおよびへの呼び出しは、またはSecondThreadではなく、メイン スレッドで実行されています。また、メソッドは何もしません。 threadAthreadBrun()

この問題でのスレッド化への言及は、単に誤った指示 (ノイズ) であり、出力とは何の関係もありません。

(メソッド名も、Java の規則に合わせて小文字で始める必要がありますFirstThread)SecondThread

于 2012-09-17T02:49:30.990 に答える
1

If I understand your question correctly, the code you give was not given as part of the homework question but rather represents you attempt to answer it. Given that, as Tudor already points out even in a correct implementation you might not see any "surprising" values, even if you run the program a few hundred times. Concurrency bugs are hard to find by testing.

My hint for understanding the correct answer is the following: Consider you are cooking two dishes for dinner with your loved one. You can cook the (spicy) main dish first and the (sweet) desert afterwards. (No threading). Or you cook them at the same time, switching between the two recipes. Both of them might refer to a pan (initially filled with air), one tells you to replace the content of your pan with eggs, the other to replace the content with oil and then add spices. Because you are a bad cook (you didn't learn about synchronization yet) you start by putting in oil, then replace everything in it by eggs, add sugar and then return to the first recipe and add spices ... what will it taste like?

于 2012-09-17T09:12:16.223 に答える
-1

他の値を取得できますが、スレッドを使用したものはランダムで制御が難しいため、プログラムを実行させるために何かを行う必要があります。コードを試すことができます。コードに何か問題があると思いますが、変更しません。わかった?

public class Threads implements Runnable
{
    int a = 0;

    public void run() { }

    public int FirstThread()
    {
        a = 3;
        Thread.sleep(2000);
        a = a + 1;

        return a;
    }

    public int SecondThread()
    {
        a = 5;
        a = a + 7;

        return a;
    }
}

またはこれを使用します

public class Threads implements Runnable
{
    int a = 0;

    public void run() { }

    public int FirstThread()
    {
        a = 3;
        a = a + 1;

        return a;
    }

    public int SecondThread()
    {
        a = 5;
        Thread.sleep(2000);
        a = a + 7;

        return a;
    }
}

やってみなよ!!!

于 2012-09-17T02:35:48.127 に答える