Results 1 to 4 of 4
  1. #1
    schliz is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Decoupled WorkingThreads interrupted by Events

    Hallo!

    I have 2 Threads, doing their job. Thread2 starts sending an Event Thread1. Thread1 reacts upon the Event, and sends back an Event, which should interrupt Thread2. And so on.

    Both threads should not poll for their Event, they should do their job, being interrupted, signal back and do their work until they get interrupted again.

    How can I achieve that?

    I tried:

    Java Code:
    public class main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		Thread1 thread1 = new Thread1();
    		Thread2 thread2 = new Thread2();
    		thread1.addEvent2EventListener(thread2);
    		thread2.addEvent1EventListener(thread1);
    		Thread t1 = new Thread(thread1);
    		Thread t2 = new Thread(thread2);
    		
    		//Start the threads
    		t1.start();
    		t2.start();
    		
    		try {
    			Thread.sleep(10000);
    			t1.stop();
    			t2.stop();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    }
    EventThread1:
    Java Code:
    import java.util.*;
    
    public class EventThread1 extends EventObject {
    	public EventThread1(Object source) { 
    		super(source); 
    		
    	} 
    
    }
    Event Thread2:
    Java Code:
    import java.util.EventObject;
    
    
    public class EventThread2 extends EventObject {
    	public EventThread2(Object source) { 
    		super(source); 
    		
    	} 
    
    }
    EventListener1:
    Java Code:
    import java.util.*;
    
    public interface EventThread1Listener extends EventListener{
    	public void eventForThread1Occurred(EventThread1 evt); 
    }
    EventListener2:
    Java Code:
    import java.util.EventListener;
    
    
    public interface EventThread2Listener extends EventListener{
    	public void eventForThread2Occurred(EventThread2 evt); 
    }
    Thread1:
    Java Code:
    public class Thread1 implements Runnable, EventThread1Listener {
    
    	//Create the listener list 
    	protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); 
    	
    	// This methods allows classes to register for EventThread1Listener 
    	public void addEvent2EventListener(EventThread2Listener listener) { 
    		listenerList.add(EventThread2Listener.class, listener); 
    		} 
    	// This methods allows classes to unregister for EventThread1Listener 
    	public void removeEvent2EventListener(EventThread2Listener listener) { 
    		listenerList.remove(EventThread2Listener.class, listener); 
    		} 
    	
    	// This private class is used to fire MyEvents 
    	void fireEventThread2(EventThread2 evt) { 
    		Object[] listeners = listenerList.getListenerList(); 
    		// Each listener occupies two elements - the first is the listener class 
    		// and the second is the listener instance 
    		for (int i=0; i<listeners.length; i+=2) { 
    			if (listeners[i]==EventThread2Listener.class) { 
    				((EventThread2Listener)listeners[i+1]).eventForThread2Occurred(evt); 
    				} 
    			} 
    		} 
    	
    	
    	
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while(true)
    		{
    			System.out.println("Thread 1");	
    			for(int i=0; i<10000000; i++)
    			{
    				for(int ii=0; ii<10000000; ii++)
    				{
    					
    				}
    			}
    		}
    	
    		
    	}
    	@Override
    	public void eventForThread1Occurred(EventThread1 evt) {
    		// TODO Auto-generated method stub
    		System.out.println("Event1 happended!");
    		fireEventThread2(new EventThread2(this)); //GM: fire back
    	}
    
    }
    Thread2:
    Java Code:
    public class Thread2 implements Runnable, EventThread2Listener{
    
    	//Create the listener list 
    	protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); 
    	
    	// This methods allows classes to register for EventThread2Listener 
    	public void addEvent1EventListener(EventThread1Listener listener) { 
    		listenerList.add(EventThread1Listener.class, listener); 
    		} 
    	// This methods allows classes to unregister for EventThread2Listener 
    	public void removeEvent1EventListener(EventThread1Listener listener) { 
    		listenerList.remove(EventThread1Listener.class, listener); 
    		} 
    	
    	// This private class is used to fire MyEvents 
    	void fireEventThread1(EventThread1 evt) { 
    		Object[] listeners = listenerList.getListenerList(); 
    		// Each listener occupies two elements - the first is the listener class 
    		// and the second is the listener instance 
    		for (int i=0; i<listeners.length; i+=2) { 
    			if (listeners[i]==EventThread1Listener.class) { 
    				((EventThread1Listener)listeners[i+1]).eventForThread1Occurred(evt); 
    				} 
    			} 
    		} 
    
    	
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while(true)
    		{
    			boolean bInit=true;
    			
    			System.out.println("Thread 2");	
    			for(int i=0; i<10000000; i++)
    			{
    				for(int ii=0; ii<10000000; ii++)
    				{
    					
    				}
    			}
    			if(bInit){ // Start Firing the event
    				fireEventThread1(new EventThread1(this));
    				bInit=false;
    			}
    		}
    			
    	}
    	
    
    	@Override
    	public void eventForThread2Occurred(EventThread2 evt) {
    		// TODO Auto-generated method stub
    		System.out.println("Event2 happended!");
    		fireEventThread1(new EventThread1(this));
    	}
    
    }
    Thank you very much in advance.
    Georg

  2. #2
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    How long did you wait?
    i haven't scrutinized all the code but the event design pattern looks fine. I just think it might take a while before the events are fired.
    Java Code:
    for(int i=0; i<10000000; i++)
    			{
    				for(int ii=0; ii<10000000; ii++)
    				{
    					
    				}
    			}
    			if(bInit){ // Start Firing the event
    				fireEventThread1(new EventThread1(this));
    				bInit=false;
    			}
    The double for loop might take a while before the first event firing occurs.

    The other problem with this long for loop is if the main thread returns from its sleep call and the threads still looping, you call t.stop() on both and the program terminates.

    You could replace the sleep and t.stop with t1.join() and t2.join(), ofcourse if your threads never end neither will your program however you should see some print outs to the console.

    Also you could get away with one listener interface that both threads implemented and one listens to the others events. Easier to read and debug.

  3. #3
    JavaJuJitZu is offline Member
    Join Date
    Jan 2010
    Posts
    26
    Rep Power
    0

    Default

    Perhaps the quickest to solve your problem is just get rid of the for loops and put a sleep(20) call instead.

    just make sure your thread sleeps for less time then the main thread sleep call.

    Anyhow good luck!

  4. #4
    schliz is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you very much for your ideas, I will try your suggestions.

    Georg:)

Similar Threads

  1. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 12:22 PM
  2. Escape Loop with interrupted() call
    By charlatain in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-23-2009, 05:52 AM
  3. Need Help with events
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 09-23-2008, 03:18 AM
  4. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •