Menu

#4 Thread usage improvement

open
None
5
2004-08-03
2004-08-03
PopolonY2k
No

The objective of this patch is optimize the Thread
usage by JaimConnection class.
Acctually the JaimConnection maintain the MyThread
active, don't matter if the connection is active or
not. This can result unexpected behaviors to JaimLib.
This modified JaimConnection class, has a destroy
method, call this method, all created threads are
destroyed. This can be usefull when a system want
destroy completely a JaimConnection instance and
re-create again (eg. after a disconnect a reconnect -
after long time).

The modified JaimConnection has marks of my
modification through the code, starting with tag
//[Leidson<] and ending with tag //[Leidson>]

This modified JaimLib is acctually running fine in
PlanetaMessenger.org Pentarou 0.2 Beta 3 and some not
wellcome behaviors like automatic disconnection after a
first failed connection, has disapeared (please see the
forum discussion at
http://sourceforge.net/forum/forum.php?thread_id=1062460&forum_id=175431\).

A suggestion for future (I could do this in future) is
kill (destroy) all Threads after disconnection
(normally by system without explicit call to destroy
method) and create all Threads again in connection
procedure.

I hope helped someone

Leidson Campos
PopolonY2k
PlanetaMessenger.org

The code bellow is the new destroy method added to lib:

/**
* Destroy provides a "real" way of interrupt
* JaimConnection instance.
* Call this method when you want release a
* JaimConnection instance from memory.
*
* eg.
* JaimConnection conn = JaimConnection();
* .....do something
* conn.destroy();
* conn = null; // We don't need this object anymore.
*
* Interrupt all threads started by JaimConnection
* constructor, this behavior will optimize the CPU
* time when we don't need the JaimConnection object
* anymore.
* Reset all list (eg. event listener) allocated
before
* by constructor.
* WARNING: This method wait for all Threads
destruction,
* call this method can freeze the caller thread for a
* time (until MyThread timeout (socket read timeout)).
*/
public synchronized void destroy() {

if( connected )
try {
disconnect();
} catch( java.io.IOException e ) {
}

dt.pleaseExit();

eventListeners.clear();
watchedBuddies.clear();
buddies.clear();
groups.clear();

// Wait for MyThread Thread finish
try {
bDestroy = true;
wait();
System.err.println( "JaimConnection.destroy() -
Object (" + toString() + ") destroyed" );
} catch( java.lang.InterruptedException ie ) {
System.err.println( "JaimConnection.destroy() -
" + ie );
} catch( java.lang.IllegalMonitorStateException
me ) {
System.err.println( "JaimConnection.destroy() -
" + me );
}
}

Discussion

  • PopolonY2k

    PopolonY2k - 2004-08-03
     
  • PopolonY2k

    PopolonY2k - 2004-08-03
    • assigned_to: nobody --> wilko
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.