SharedBufferTest.java :  » Game » dynamicbuttonsoccer » test » Java Open Source

Java Open Source » Game » dynamicbuttonsoccer 
dynamicbuttonsoccer » test » SharedBufferTest.java

package test;

import common.SharedBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xissburg
 */
public class SharedBufferTest
{
    public static void main(String[] args)
    {
        final SharedBuffer buffer = new SharedBuffer();

        //producer
        new Thread(new Runnable() {
            public void run()
            {
                int i=0;

                while(true)
                {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(SharedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    buffer.set(i++);
                }
            }
        }).start();

        //consumer
        new Thread(new Runnable() {
            public void run()
            {
                while(true)
                {
                    Object o = null;
                    try {
                        o = buffer.get(true);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(SharedBufferTest.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    if(o != null)
                        System.out.println(o);
                }
            }
        }).start();

        System.out.println("Producer and Consumer threads started");
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.