简体   繁体   中英

How to print non repeating element by two threads?

i want to print non repeating elements from the same resource by two threads.

here in below code , am printing repeated elements

class TestSleepMethod1 extends Thread{  
    public void run(){  
        for(int i=1;i<5;i++){  
            try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  
            System.out.println(i);  
        }  
    }

    public static void main(String args[]){  
        TestSleepMethod1 t1=new TestSleepMethod1();  
        TestSleepMethod1 t2=new TestSleepMethod1();  

        t1.start();  
        t2.start();  
    }  
}  

The output :

1 1 2 2 3 3 4 4 . I want, if one thread prints "1" other thread should not print "1" again instead it should print 2 . How to achieve this condition? Thanks.

You can have a Queue (eg:BolockingQueue) and add all the numbers to it. Then notify the thread after the addition which should take the values from the queue one by one. This will help you to achieve the result you desire. Refer http://tutorials.jenkov.com/java-concurrency/blocking-queues.html

In your case very unlikely to happen because of the thread sleeping.

Try to use different sleeping interval:

class TestSleepMethod1 extends Thread {
    private final long sleepingInterval;

    private TestSleepMethod1(long sleepingInterval) {
        this.sleepingInterval = sleepingInterval;
    }

    public void run(){  
        for(int i=1;i<5;i++){  
            try{Thread.sleep(sleepingInterval);}catch(InterruptedException e){System.out.println(e);}  
            System.out.println(i);  
        }  
    }

    public static void main(String args[]){  
        TestSleepMethod1 t1=new TestSleepMethod1(500);  
        TestSleepMethod1 t2=new TestSleepMethod1(300);  

        t1.start();  
        t2.start();  
    }  
}  
try 



    static int i =1;
    public void run(){ 
        for(;i<5;){  // you can also use while(i<5)
            try{
                Thread.sleep(500);

            }catch(InterruptedException e){
                System.out.println(e);
            }  
            System.out.println(i++); 
        }  
    }  
    public static void main(String args[]){  
        TestSleepMethod1 t1=new TestSleepMethod1();  
        TestSleepMethod1 t2=new TestSleepMethod1();  

        t1.start();  
        t2.start();  
    }  


output 
1
2
3
4

You must used a shared and thread safe structure which enforce atomicity of the operations.

An AtomicInteger fits your need. AtomicInteger#incrementAndGet is an atomic operation. By consequence, only one thread will, for a distrinct value, increment this value and return it:

class TestSleepMethod1 extends Thread{  
    private static final AtomicInteger counter = new AtomicInteger(0);
    public void run(){  
        for(int i=1;i<5;i++){  
            try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}  
            System.out.println(counter.incrementAndGet());  
        }  
    }

    public static void main(String args[]){  
        TestSleepMethod1 t1=new TestSleepMethod1();  
        TestSleepMethod1 t2=new TestSleepMethod1();  

        t1.start();  
        t2.start();  
    }  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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