0

我有一个数组: int[] arr = {5,4,3,1,2};

 I want to do like this::

 5 should be read by thread one
 4 should be read by thread two
 3 should be read by thread one
 1 should be read by thread two
 2 should be read by thread one

我已经尽力了这个简单的程序:

package com.techighost.create.deadlock;

public class ArrayReading implements Runnable {

    volatile int index = 0;

    int[] arr;

    public ArrayReading(int[] arr) {
        this.arr = arr;
    }

    @Override
    public void run() {
        synchronized (arr) {
            for (;index<=(arr.length-1);) {
                if (index % 2 == 0  && Thread.currentThread().getName().equals("Thread-One")) {
                    System.out.println(arr[index] + " " + Thread.currentThread().getName());
                    index++;
                    arr.notify();

                } else if (index % 2 != 0 && Thread.currentThread().getName().equals("Thread-Two")) {
                    System.out.println(arr[index] + " " + Thread.currentThread().getName());
                    index++;
                    arr.notify();

                }else{
                    System.out.println("In else " + Thread.currentThread().getName());
                    try {
                        arr.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        int[] arr = { 5, 4, 3, 1, 2 };
        ArrayReading arrayReading = new ArrayReading(arr);
        Thread t = new Thread(arrayReading);
        t.setName("Thread-One");
        Thread t1 = new Thread(arrayReading);
        t1.setName("Thread-Two");

        t.start();
        t1.start();

        t.join();
        t1.join();
    }
}

我认为这个线程名称检查不应该在那里?任何机构请建议可以做些什么来删除这个检查

4

6 回答 6

3

您可以使用@zzk.Program 提到的条件,因为这可以是

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class PrintSequentially {

private final int[] items;
private final ReentrantLock lock;
private final Condition notEven;
private final Condition notOdd;

private int currentCount = 0;

public PrintSequentially(int[] items) {
    this.items = items;
    this.lock = new ReentrantLock();
    this.notEven = lock.newCondition();
    this.notOdd = lock.newCondition();
}

public void printSeq() throws InterruptedException {

    try {
        lock.lockInterruptibly();
        while (currentCount < items.length) {
            if (currentCount % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + ":"
                        + items[currentCount++]);
                if (currentCount < items.length)
                    notEven.await();
                notOdd.signal();
            } else {
                System.out.println(Thread.currentThread().getName() + ":"
                        + items[currentCount++]);
                notEven.signal();
                if (currentCount < items.length)
                    notOdd.await();
            }
        }

    } finally {
        lock.unlock();
    }
}

}

驱动程序是

public static void main(String[] args) {
    int arr[] ={1,2,3,4,5};
    final PrintSequentially p = new PrintSequentially(arr);

    Runnable r1 = new Runnable() {
        @Override
        public void run() {
            try {
                p.printSeq();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    Runnable r2 = new Runnable() {
        @Override
        public void run() {
            try {
                p.printSeq();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    Thread th1 = new Thread(r1);
    th1.setName("thread 1");
    th1.start();

    Thread th2 = new Thread(r2);
    th2.setName("thread 2");
    th2.start();

}

在这里,您可以添加任意数量的线程。它将按顺序打印。

于 2016-10-24T19:27:14.313 回答
2

你可以使用条件。线程 1 应等待条件索引 % 2 == 0,线程 2 应等待条件索引 % 2 == 1。

查看此链接以了解如何使用条件

于 2013-02-26T21:17:14.563 回答
1

我知道这可能是某种让你的脚湿透的线程应用程序,但它存在许多问题,使其不太理想。

  1. 使用线程的重点是异步操作。希望您的线程处理数组中的所有其他条目听起来就像您正在划分工作,但这可能比单线程运行得慢,因为同步完成彼此。线程的性质也意味着可以在“1”之前打印“2”。这是一件好事,因为您不会放慢线程以使它们井井有条。

  2. 您的代码在这里有一些竞争条件。例如,一个线程可以处理列表的最后一个元素并转到,wait但另一个线程可能已经完成了列表并且不会在那里notify。我敢打赌你的应用程序经常挂在最后。

  3. 您应该考虑使用执行器服务并为每个条目提交作业。这是执行大多数线程任务的最佳方式:

    // create a thread pool with 2 workers
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    for (int entry : arr) {
        threadPool.submit(new `(entry));
    }
    // once we have submitted all jobs to the thread pool, it should be shutdown
    threadPool.shutdown();
    // to wait for the jobs to finish you do
    threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    ...
    

    然后,您ArrayReading获取条目而不是整个数组,并且可以独立处理它们。

  4. 最后,正如其他人已经提到的,您可以传递一个boolean even标志来让每个线程处理偶数(如果为真)或奇数(如果为假)项目。

    Thread t1 = new Thread(new ArrayReading(arr, true));
    Thread t2 = new Thread(new ArrayReading(arr, false));
    
于 2013-02-26T21:40:59.610 回答
1

您可以使用waitnotify这样的线程间通信:

class ReadNum
{
    int arr[];
    private volatile int counter = 0;
    public ReadNum()
    {
        counter = 0 ;
    }
    public ReadNum(int size)
    {
        arr = new int[size];
        for (int i = 0; i < size ; i++)
        {
            arr[i] = i;
        }
    }
    public void setArray(int[] arr)
    {
        counter = 0;
        this.arr = arr;
    }
    public synchronized void  readOdd()
    {
        while (counter < arr.length)
        {
            if (counter % 2 != 0)
            {
                System.out.println(Thread.currentThread().getName()+":->"+arr[counter]);
                counter++;
            }
            notify();
            try{
                wait();
            }catch(Exception ex){ex.printStackTrace();}
        }
        notify();//So that other EvenThread does'nt hang if OddThread completes earlier
    }
    public synchronized void  readEven()
    {
        while (counter < arr.length)
        {
            if (counter % 2 == 0)
            {
                System.out.println(Thread.currentThread().getName()+":->"+arr[counter]);
                counter++;
            }
             notify();
            try{
                wait();
            }catch(Exception ex){ex.printStackTrace();}
        }
        notify();//So that other OddThread does'nt hang if EvenThread completes earlier
    }
}
public class SequenceRead
{
    public static void main(String st[])
    {
        final ReadNum rn = new ReadNum();
        int arr[]= {1,2,34,78,99,45,4545,987,343,45};
        rn.setArray(arr);
        Thread th1 = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                rn.readEven();
            }
        },"EvenReadThread");
        Thread th2 = new Thread( new Runnable()
        {
            @Override
            public void run()
            {
                rn.readOdd();
            }
        },"OddReadThread");
        th2.start();th1.start();
    }
}

更新

这是您要求的有关种族条件的解释。

竞争条件“在这种情况下,多个线程可以访问相同的资源(通常是对象的实例变量),并且如果一个线程在应该是原子的操作完成之前“竞争”或“潜入”太快,可能会产生损坏的数据。因此,程序的输出是不可预测的,因为它取决于访问同一资源的各个线程的启动、执行和完成的顺序或时间。”

例如考虑下面给出的代码:

class Race
{
    private int counter;
    public void printCounter()
    {
        while(counter < 100)
        {
            try
            {
                Thread.sleep(10);//Added to show Race Effect.
            }
            catch (Exception ex){}
            counter = counter + 1;
        }
        System.out.println(Thread.currentThread().getName() +" : "+counter);//If we don't consider Race condition then the Output should be 100 for all threads. 
    }
}
public class MainClasss
{
    public static void main(String st[])
    {
        final Race race = new Race();
        Thread[] th = new Thread[2];
        //Creating 2 threads to call printCounter of object race
        for (int i = 0 ; i < th.length ; i++)
        {
            th[i] = new Thread( new Runnable()
            {
                public void run()
                {
                    race.printCounter();
                }
            }, "Thread"+i);
        }
        //Starting all Threads
        for (Thread thr : th )          
        {
            thr.start();
        }
    }
}

这是我得到的输出,它可能因您的系统而异。

Thread1 : 100
Thread0 : 101

所有线程都没有按预期打印 100 !!!为什么?因为程序无法控制一个正在执行的线程何时会被另一个线程抢占。这完全取决于 JVM 线程调度程序。
上述输出的一种可能解释如下:

  1. 在 counter = 99 处,Thread1 潜入 while 循环并休眠了 10 毫秒。
  2. JVM 调度程序现在由 Thread0 抢占 Thread1。
  3. Thread1 进入“while”循环,因为它发现 counter < 100
  4. 在 Thread.sleep Thread0 被 Thread1 抢占。
  5. Thread1 将计数器加 1。
  6. Thread1 将计数器值打印为 100 并完成。
  7. Thread0 继续执行并将计数器加 1 并使计数器 = 101
  8. Thread0 将计数器值打印为 101 并完成。

这是Race Condition的现场展示。
为避免这种竞争条件,您应该将该ReadNum方法设置为同步的,这样当一个线程进入该方法时,它会获取监视器并成为同步方法的所有者。并且该线程仅在它以原子方式完成所有操作后才被抢占。我希望它现在可以让您对 Race Condition 有一个很好的了解。

于 2013-02-26T21:32:09.327 回答
1

使用 runnable 中的另一个参数字段告诉它读取偶数或奇数索引,创建 runnable 的两个实例,一个用于偶数,一个用于奇数。设置一个ExecutorService至少有两个线程,执行runnables。他们可能完成得太快而无法获得不同的线程。没有测试这个。

于 2013-02-26T21:16:15.653 回答
0

这是您正在寻找的代码....

public class ThreadConcurrent  {
    int []array=new int[]{0,1,2,3,4,5,6,7,8,9};
    volatile int i=0;

public  void checkSum() {
    synchronized (this) {
        for(;i<array.length;){
            System.out.println("thread name "+Thread.currentThread().getName()+ "  : "+array[i]);
            i++;
            notify();
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }
}

public static void main(String[] args) {

    final ThreadConcurrent er=new ThreadConcurrent();       
    Thread t1=new Thread(new Runnable() {

        @Override
        public void run() {
            er.checkSum();

        }
    }, "T1");
    Thread t21=new Thread(new Runnable() {

        @Override
        public void run() {
            er.checkSum();

        }
    }, "T2");
    t1.start();
    t21.start();
}

}

于 2013-02-27T08:48:13.350 回答