显示在java中与两个线程同步的用法

5lhxktic  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(252)

我一直在看多线程教程和那些关于同步的特定教程,但是我还没有实现我需要的东西。
这是一个在我的程序中同步发生的展示。
基本上,我有一个类从另一个类继承了一些函数,这些函数需要同步,以便两个线程不会同时修改一个对象(没有数据损坏)。
我以前实现的代码没有synchronize关键字,所以我可以设法看到数据损坏的发生。

EditOptionsMethods e1;
    int threadNo;

    public EditOptions() {
        e1 = new BuildAuto();
        run();
    }

    public void run() {
        System.out.println("Thread running.");

        switch (threadNo) {
            case 0:

                break;

            case 1:
                break;
        }

    }

    public void setOptions(String optSetName1, String desiredOption1, String optSetName2, String desiredOption2) {
        e1.s_Option(optSetName1, desiredOption1); // 
        e1.s_Option(optSetName2, desiredOption2);
    }

必须同步s\u选项,这样两个线程都不会出现。我首先将它不同步,然后我可以初始化一个循环(高索引量,假设1000,然后我与第一个线程相加,与第二个线程相减),以查看发生的损坏作为一个例子。
但我找不到一个方法来表达这个。
如果有人知道我如何实现这个,那就太棒了。

2ledvvac

2ledvvac1#

首先创建一个将容纳线程的结构:

List<Thread> threads = new ArrayList<>();

然后初始化它,并将工作分配给线程:

int total_threads = 2;
   for(int i = 0; i < total_threads; i++){
       Thread thread  = new Thread(() -> {
           // the work to be done by the threads
       });
       threads.add(thread);
   }

运行线程:

threads.forEach(Thread::start);

最后,等待线程完成其工作:

threads.forEach(t -> {
       try {
                t.join();
       } catch (InterruptedException e) {
                e.printStackTrace();
        }
   });

通过一个运行示例:

import java.util.*;

public class SomeClass {

    private int x;

    public void addX(int y){ x = x + y;}

    public static void main(String[] args) {
           SomeClass example = new SomeClass();
           List<Thread> threads = new ArrayList<>();
           int total_threads = 2;
           for(int i = 1; i < total_threads + 1; i++){
               final int threadID = i;
               Thread thread  = new Thread(() -> {
                   for(int j = 0; j < 1000; j++ )
                    example.addX((threadID % 2 == 0) ? -threadID : threadID);
               });
               threads.add(thread);
           }
           threads.forEach(Thread::start);
           threads.forEach(t -> {
               try {
                        t.join();
               } catch (InterruptedException e) {
                        e.printStackTrace();
                }
           });
           System.out.println(example.x);
    }
}

现在尝试运行上面的示例,并同步线程完成的工作:

Thread thread  = new Thread(() -> {
   for(int j = 0; j < 1000; j++ )
       example.addX((threadID % 2 == 0) ? -threadID : threadID);
   });

与:

Thread thread  = new Thread(() -> {
 synchronized(example){ // <-- adding synchronization 
    for(int j = 0; j < 1000; j++ )
      example.addX((threadID % 2 == 0) ? -threadID : threadID);
   }
 });

对于使用同步的版本,您可以根据需要多次运行代码,输出将始终保持同步 -1000 (使用2个螺纹)。然而,对于没有同步的版本,由于变量更新期间发生的竞争条件,输出将是不确定的 x .
而不是使用 Thread 直接初始化,您可以选择更高的抽象,即执行器:

public class SomeClass2 {

  private int x;

  public void addX(int y){x = x + y;}

  public static void main(String[] args) {
      SomeClass2 example = new SomeClass2();
      int total_threads = 2;
      ExecutorService pool = Executors.newFixedThreadPool(total_threads);
      pool.execute(() -> {
         synchronized (example) {
            parallel_task(example, -2);
        }
      });
      pool.execute(() -> {
        synchronized (example) {
            parallel_task(example, 1);
         }
      });

      pool.shutdown();
      try { pool.awaitTermination(1, TimeUnit.MINUTES);} 
      catch (InterruptedException e) {e.printStackTrace();}  
      System.out.println(example.x);
}

private static void parallel_task(SomeClass2 example, int i) {
    thread_sleep();
    for (int j = 0; j < 1000; j++)
        example.addX(i);
}

private static void thread_sleep() {
    try { Thread.sleep(1000); } 
    catch (InterruptedException e) {
        e.printStackTrace();
    }
 }
}

我添加了 thread_sleep() 以确保两个并行任务不会被同一线程从线程池中拾取。

相关问题