library event handler threads

Hi

I am creating a small class-library for my own projects involving some heavy IO operations. We all know this take some time and therefore should not happen on the GUI thread. My library therefor start a new thread when it start working, but my question is; Should I code so that the library automaticly does the SwingUtils.invokeLater internally or is this something that should be done by the user of the library? Or is there any other (better) way to make sure the events triggered during the library actually happens inside the GUI thread? Want to avoid possible concurrency problems and race conditions, but on the other hand, I may use this library without using swing.

The code for the library (am just writing some architecture as I havent starting writing the actual code yet) may look like this:

interface someeventlistener

{

publicvoid eventtriggered();

}

class Libraryimplements Runnable

{

private Vector<someeventlistner> listeners =new Vector<someeventlistener>();

public Library()

{

(new Thread(this)).start();

}

publicvoid run()

{

while(alot_of_operations_isnt_finished)

{

do_some_processing();//just to explain

trigger_library_events();

}

}

privatevoid trigger_library_events()

{

for(int i=0;i<listeners.size();i++)

((someeventlistener)listeners.get(i)).eventtriggered();//should this be inside invokeLater ?

}

publicvoid addEventListener(someeventlistener e)

{

listeners.add(e);

}

}

PS: This may or may not be posted wrong. I thought it had more to do with concurrency than swing/gui, but maybe I am mistaken?>

[2823 byte] By [invictus2a] at [2007-11-26 14:13:16]
# 1

All you have right now is a fantasy.

Before you get to the code, try planning. Top down design usually works well. Then try reading some literature on threading, like:

http://java.sun.com/docs/books/tutorial/essential/concurrency/

for the basics

and why not to use "this" in constructors:

http://www-128.ibm.com/developerworks/java/library/j-jtp0618.html

cooper6a at 2007-7-8 2:02:15 > top of Java-index,Core,Core APIs...
# 2

This is interesting reading, but it doesnt quite answer the problem I am facing. Should the library itself provide the invokeLater() method or what is the best way to make sure the event triggered is scheduled on the applications main thread/gui thread? This is kind of important for me to get right.

Let me clear this up with an example. When I create a JButton (or any other component) and add an actionlistener this event will be put in the awt event-queue (i checked with Thread.currentThread().getId and getName). How can I create such AWT-events from MY library? I was thinking of the swing utils invokeLater method, but this seem to be a bit swing-bound for my taste.

I am really sorry for the bad explanation of this.

invictus2a at 2007-7-8 2:02:16 > top of Java-index,Core,Core APIs...
# 3

I figured this question out on my own with a little help from the reply. However I have another question related to the same area:

Lets say I have a thread for IO operations. In my case it will probably be a socket waiting for data or similar. This thread would then block until some data was available. However, I would also like the code to continue if there wasnt any data , but some other part of the code sent a signal (like for instance "shut down the socket now" to be executed IN the socket thread). Like a custom event queue that also work with sockets/io. Is there any obvious approach to this?

invictus2a at 2007-7-8 2:02:16 > top of Java-index,Core,Core APIs...
# 4
Why not use Java non blocking IO API's... http://java.sun.com/developer/technicalArticles/releases/nio/
chaos_begins_herea at 2007-7-8 2:02:16 > top of Java-index,Core,Core APIs...
# 5

Yeah that was what I was thinking, but still I need a different thread to waiting for socket events. So this is why I ask if there is any way I can also make this select() method reacting to some code-based signal to perform all sockets operations (shutdown, sending data, etc) within the same thread as waiting for socket data.

I dont find shutting down a socket that is in use by another thread that good coding practice.

invictus2a at 2007-7-8 2:02:16 > top of Java-index,Core,Core APIs...
# 6
To put it in another way: is there any way I can trigger a blocking select() to continue execution even though there is no socket event?
invictus2a at 2007-7-8 2:02:16 > top of Java-index,Core,Core APIs...