简体   繁体   English

如何同步不同类中的两个线程

[英]How to sync two threads in different classes

I need to have two threads synchronized such that both cannot run concurrently. 我需要同步两个线程,以便两个线程不能同时运行。 Once called, they need to run so if the other thread is running, they need to wait until the other one is done and then run after it. 一旦被调用,它们需要运行,因此如果另一个线程正在运行,则需要等到另一个线程完成后再运行。 I know I can use join() but my question involves threads in different classes with no reference to each other. 我知道我可以使用join(),但是我的问题涉及不同类中的线程,彼此之间没有引用。 Is it a good idea to make the threads static class variables so they both can access each other? 使线程成为静态类变量以便它们都可以相互访问是一个好主意吗?

One thread (t1) is called from a method in the Main Activity and the other thread (t2) is inside an AsyncTask: 一个线程(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
        }
    }
}

I executed the synchronization by adding static class thread variables and added a sleep factor so I could test: 我通过添加静态类线程变量并添加了睡眠因子来执行同步,因此可以测试:

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();
                }
            }
        }
    }
}

You can use two blocking queues, one for each thread. 您可以使用两个阻塞队列,每个线程一个。 They would each start working when they read from their queue, and they stop by writing to the other thread's queue. 当他们从队列中读取时,它们将各自开始工作,并通过写入另一个线程的队列而停止。 That way, there's always one that's active. 这样,总有一个处于活动状态。 They could pass just a token, or an object with data. 他们可以只传递令牌,也可以传递带有数据的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM