The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

data reciever thread gets interrupted by AWTThread

Posted by shekar on July 19, 2001 at 11:17 AM

my app starts a reciever thread which indefinitely recieves a data/message from a server [written in c]on UDP port. these messages are split into String array[10] and they are to be added as a new row of a JTable through a TableModel. But reciever thread shall never get interrupted at any cost by AWTThread that adds a row to a Table. I want the reciever thread to just recieve message and let the main GUI that houses the JTable to handle dispathing of event lets say setTableRow(String row[]). Am afraid if i implement a producer/consumer pair, it would lock the reciever Thread which is not acceptable as the data is coming at a speed of 10 to 15 records per second.
I think EventDelegation model would offer a better design approach. But i have no knowledge of how to implement delegation model for this scenario.
existing problem: while new rows are added reciever thread missing some of packets. i suspect the AWTThread is taking more time..interrupting recieverThread.

// Following code does not contain the working module.
// this is the look of MY App's design.
// the server sends data at apretty high speed about 10 to 15 records per second
// My reciever thread is a kind of selfish thread, doesnt sleep at all.
// is there anything wrong in design approach for dispatching the event on MAinGUI Frame
// I want reciever thread to just recieve the data and let the frame handle
// processing of events for displaying the rows

class DataReciever{
JFrame frame
// gets instantiated by mainGUI
public void dataReciver(String host,int port,JFrame frm)
{
frame = frm;
// basic connection to the reciever
// register the port on which reciever listens
//set up Datagram socket
// start the RecieverThread
}

// inner reciver Thread class
class reciever extendsThread
{
public void run()
{
byte[] buf = new byte[1024];

DatagramPacket packet = new DatagramPacket(buf, buf.length);

sock.receive(packet); // block

ByteArrayInputStream bstream =
new ByteArrayInputStream(packet, 1,
packet.getLength()-1);

parseMessage(bstream);
}

parseMessage(ByteArrayInputStream stream)
{
//message parsing..
String row = new String[9];
row = message;

frame.callDispatchEvent(row);
}
};
public void disconnect(){}
}
/***************************************************************/

class mainUI
{

mainGUI()
{

enableEvents(UserEvent.USER_EVENT_MASK);

addUserEventListener(new UserEventAdapter()
{
public void showEvent(UserEvent ue)
{
// method of Table class to set add rows to the TableModel
Table.setRows((String [])ue.getObject());
}
});

table = new Table();
// set the column title
table.setColumns(Columns);
JScrollPane tableScrollPane = new JScrollPane(table);
// basic frame window
JFrame frame = new JFrame("");
// handle closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
disconnect();//disconnect from the reciever
System.exit(0);
}
});

frame.getContentPane().add(tableScrollPane);//add table
// show frame
// call DataReciever to run reciever Thread with (server name, port number, frame)
}
public synchronized void addUserEventListener(UserEventListener l)
{
userEventListener = UserEventMulticaster.add(userEventListener, l);
}


public synchronized void removeUserEventListener(UserEventListener l) {
userEventListener = UserEventMulticaster.remove(userEventListener, l);
}


public void processEvent(AWTEvent e)
{
if(e instanceof UserEvent)
{
if (userEventListener != null)
{
if(e.getID() == UserEvent.SHOW_EVENT)
{
userEventListener.showEvent((UserEvent)e);
}
return;
}
}
super.processEvent(e);
}
public void callDispatchEvent(String row[])
{
UserEvent ue = new UserEvent(this,UserEvent.SHOW_EVENT,row);

frame.dispatchEvent(ue);
}
}
/***************************************************************/

public class UserEvent extends AWTEvent {

public static final int SHOW_EVENT = RESERVED_ID_MAX+1;
public static final int USER_EVENT_MASK = 0x10000;

Object sObject[] = new Object[9];

public UserEvent(Object source,int id,Object s[]){
super(source,id);
sObject =s;
}

public Object[] getObject(){
return sObject;
}

public String paramString() {
String typeStr;
switch(id) {
case SHOW_EVENT:
typeStr = "SHOW_EVENT";
//typeStr += (",Record = "+(String)sObject);
break;
default:
typeStr = "unknown type";
}
return typeStr;
}

}

/***************************************************************/

public interface UserEventListener extends EventListener {
public void showEvent(UserEvent e);
}
/***************************************************************/

public abstract class UserEventAdapter implements UserEventListener {
public void showEvent(UserEvent ue){}

}
/***************************************************************/

public class UserEventMulticaster extends AWTEventMulticaster implements UserEventListener {

protected UserEventMulticaster(EventListener a, EventListener b) {
super(a,b);
}

public void showEvent(UserEvent e) {
((UserEventListener)a).showEvent(e);
((UserEventListener)b).showEvent(e);
}

public static UserEventListener add(UserEventListener a, UserEventListener b) {
return (UserEventListener)addInternal(a, b);
}

public static UserEventListener remove(UserEventListener l, UserEventListener oldl) {
return (UserEventListener) removeInternal(l, oldl);
}

}
/***************************************************************/

class Table
{

protected static JTable m_table;
protected static TableModel m_data;

public Table()
{
// initalize TableModel and JTable
}
public static void setColumns(String columns[])
{
// set up column header
}
public static void setRows(final String rows[])
{
m_data.setTableRows(rows);

int temp = m_data.getColumnCount();

for(k = 0;k {
model.getColumn(k).setCellRenderer(new TabIconRenderer());
m_table.tableChanged(new TableModelEvent(m_data));

}
}
public JTable getTable()
{

return m_table;
}
}

/***************************************************************/

class TableModel extends DefaultTableModel
{
int rowCount;

public static Vector m_columns;

protected static Vector m_vector;

public static int m_columnsCount = ( m_columns==null) ? 0 :
m_columns.size();


TableModel()
{
m_columns = new Vector();// coulmns
m_vector = new Vector();// rows

m_vector.removeAllElements();
m_columns.removeAllElements();

m_columnsCount = ( m_columns==null) ? 0 : m_columns.size();

}

public void setColumn(){}//

public void setTableRows(String rows[])
{
m_vector.add(0,new RowObject(rows));
}

public Object getValueAt(col,row)
{
return value;//
}
}

/***************************************************************/

public class TabIconRenderer extends DefaultTableCellRenderer
{

// this render the icons and backGound color for table row depending on certain values in row

}
/******************************END*********************************/
Any suggestion with code snippet wud be of greate help.
thanks in advance.
Shekar.





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us