【问题标题】:How to sync two threads in different classes如何同步不同类中的两个线程
【发布时间】:2018-12-22 01:51:47
【问题描述】:

我需要同步两个线程,这样两个线程就不能同时运行。一旦被调用,它们就需要运行,所以如果另一个线程正在运行,它们需要等到另一个线程完成然后在它之后运行。我知道我可以使用 join() 但我的问题涉及不同类中的线程,彼此之间没有引用。将线程设为静态类变量以便它们可以相互访问是个好主意吗?

一个线程 (t1) 从 Main Activity 中的方法调用,另一个线程 (t2) 在 AsyncTask 内:

public class MainActivity extends AppCompatActivity
    {
        public void someMethod()
        {
            // code
            Thread t1 = new Thread(() ->
        {
            // run thread code
        });
        t.start();
        try
            {
            t.join();
            }
        catch (InterruptedException e)
            {
            e.printStackTrace();
            }

            // someMethod follow-up code
        }

    }

    public class FetchData extends AsyncTask<Void, Void, Void>
    {
        protected final Void doInBackground(Void... params)
        {
            // code
            Thread t2 = new Thread(() ->
        {
            // run thread code
        });
        t.start();
        try
            {
            t.join();
            }
        catch (InterruptedException e)
            {
            e.printStackTrace();
            }

            // follow-up code
        }
    }
}

【问题讨论】:

  • 如果你觉得需要同步两个线程,你可能需要重新思考你的应用的逻辑。
  • 大声笑在多线程img中没有同步线程是必不可少的

标签: java android multithreading android-asynctask


【解决方案1】:

我通过添加静态类线程变量来执行同步,并添加了一个睡眠因子,以便我可以测试:

public class MainActivity extends AppCompatActivity implements Interface.InterfaceCommon
    {
    public static Thread t1;
    FetchData fetchData;

    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
        t1 = new Thread();
        fetchData = new FetchData();
        }
    public void someMethod()
        {
        Runnable r = () ->
            {
            try
                {
                Thread.sleep(5000);
                }
            catch (InterruptedException e)
                {
                e.printStackTrace();
                }
            /// Some code
            };
        t1 = new Thread(r);
        try
            {
            FetchData.t2.join();
            t1.start();
            }
        catch (InterruptedException e)
            {
            e.printStackTrace();
            }
        }
    }

    public class FetchData extends AsyncTask<Void, Void, Void>
        {
        public static Thread t2;
        public FetchData()
            {
            t2 = new Thread();
            }
        protected final Void doInBackground(Void... params)
            {
            Runnable r = () ->
                {
                try
                    {
                    Thread.sleep(5000);
                    }
                catch (InterruptedException e)
                    {
                    e.printStackTrace();
                    }
                    /// Some code
                };
            t2 = new Thread(r);
            try
                {
                MainActivity.t1.join();
                t2.start();
                }
            catch (InterruptedException e)
                {
                e.printStackTrace();
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以使用两个阻塞队列,每个线程一个。当他们从他们的队列中读取时,他们每个人都会开始工作,并且他们会通过写入另一个线程的队列来停止。这样,总会有一个活跃的。他们可以只传递一个令牌,或者一个带有数据的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      相关资源
      最近更新 更多