The Artima Developer Community
Sponsored Link

Java Answers Forum
method that is not working inside thread

4 replies on 1 page. Most recent reply: Nov 14, 2003 5:47 PM by Pratheeba

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Pratheeba

Posts: 11
Nickname: pratheeba
Registered: Sep, 2003

method that is not working inside thread Posted: Nov 13, 2003 9:15 AM
Reply to this message Reply
Advertisement
Hi,

I have one method(which is public & synchronized ) in my class named Userinterface.I want to use that method in another class which is having thread in it. In that, inside run method if i called the method of Userinterface class, it is going into the method...But it shows the local variable values as zero for all...
But if i called the method in the same userinterface class it works fine...
I don't know why it's happening...It's urgent...May it's silly mistake i don't know.Can any one help me?

Thanks
pratheeba


Shekhar

Posts: 11
Nickname: shekhar4u
Registered: Aug, 2003

Re: method that is not working inside thread Posted: Nov 13, 2003 10:10 PM
Reply to this message Reply
Can u post the source code?

Pratheeba

Posts: 11
Nickname: pratheeba
Registered: Sep, 2003

Re: method that is not working inside thread Posted: Nov 14, 2003 7:53 AM
Reply to this message Reply
hi, Here is my code

public class UserInterface extends Frame
{
public DefaultTableModel tableModel;
JScrollPane tablePane;
public JTable table;
String catTableName,catTableDescription;
Vector columnNames;
Vector catData,catAllData;
Vector vecCatData;
JScrollPane tableScroll;
public String findTicker,input;
Client c;
Socket s;
HashMap allTicker = new HashMap();
int rowCountdelete ;
/** Constructor
*/
public UserInterface()
{
setLayout(new BorderLayout());
Vector vecStockdata = new Vector();
Vector columnNames = new Vector();
String col1 ="Name";
String col2 ="Symbol";
String col3 ="Trade Market";
String col4 ="Last Trade Value";
String col5 ="52 weeks low";
String col6 ="52 weeks high";
columnNames.add(col1);
columnNames.add(col2);
columnNames.add(col3);
columnNames.add(col4);
columnNames.add(col5);
columnNames.add(col6);
int rowCount = 0;
int columnCount = 6;
tableModel =new DefaultTableModel(vecCatData,columnNames);
table = new JTable(tableModel);


TableColumn column = null;
for(int i = 0; i < 6; i++)
{
column = table.getColumnModel().getColumn(i);
if(i==0)
{
column.setPreferredWidth(200);
}
else if(i==1)
{
column.setPreferredWidth(30);
}
else
{
column.setPreferredWidth(50);
}
}
table.setPreferredScrollableViewportSize(new Dimension(900, 383));
tableScroll = new JScrollPane(table);
final JRadioButton addButton = new JRadioButton("Get");
final JRadioButton deleteButton = new JRadioButton("Delete");
JPanel radioPanel = new JPanel(new FlowLayout());
radioPanel.add(addButton);
radioPanel.add(deleteButton);
add(radioPanel, BorderLayout.SOUTH);
add(tableScroll,BorderLayout.CENTER);

public synchronized void getAllTableValue()
{
System.out.println("Coming inside method...");
int i;
Object tickRef = null;
Socket tableSocket = null;
int rowCount;
rowCount = table.getRowCount();
System.out.println("THE ROWCOUNT " + rowCount);
System.out.println("COUNT " + tableModel.getRowCount());
for(i=0;i<rowCount ;i++)
{
tickRef = table.getValueAt(i, 1);
String tickerRefresh = tickRef.toString();
System.out.println("Ticker for refreshing is " + i + "--" + tickerRefresh);
StockInfo sinfo = new StockInfo(tickerRefresh);
try{
tableSocket =new Socket(InetAddress.getLocalHost(),1167);
OutputStream ostable = (tableSocket.getOutputStream());
ObjectOutputStream oostable = new ObjectOutputStream(ostable);
oostable.writeObject(tickerRefresh);
InputStream istable = tableSocket.getInputStream();
ObjectInputStream iostable = new ObjectInputStream(istable);
sinfo = (StockInfo) iostable.readObject();
table.setValueAt(sinfo.getStockName(),i,0);
table.setValueAt(sinfo.getTicker(),i,1);
table.setValueAt(sinfo.getTradeMarket(),i,2);
table.setValueAt(sinfo.getLastTradeValue(),i,3);
table.setValueAt(sinfo.get52WeekLow(),i,4);
table.setValueAt(sinfo.get52WeekHigh(),i,5);
System.out.println("Table refreshed successfully...");
}
catch(UnknownHostException ue)
{
System.out.println("I can't find host");
}
catch(Exception ee)
{
System.out.println("Caught in exception");
System.out.println(ee.getMessage());
}
}

}
}

My Thread class code:

public class OurTableThread extends Thread implements Serializable
{

UserInterface uitable ;
public OurTableThread(){}

public OurTableThread(UserInterface uitable)
{
this.uitable = uitable;
}
public synchronized void run(){
while(true)
{
refresh();
}
}

public synchronized void refresh()
{
try{
uitable.getAllTableValues();
sleep(90000);
}
catch(InterruptedException ei){}
catch(NullPointerException ni){}
}
}

My Main program in which i am calling those to class:

public class OurClient
{

public static void main(String[] args) {

UserInterface ui = new UserInterface();
new OurTableThread(ui).start();
ui.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ui.setSize(900, 300);
ui.setVisible(true);
ui.setTitle("Quote Server");
}
}

Here i pasted my part of UserInterface class and Main class...I can't put full code... I hope this will be understandable...Thanks.Expecting reply...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: method that is not working inside thread Posted: Nov 14, 2003 10:38 AM
Reply to this message Reply
Swallowing exceptions in empty catch clauses is probably not a good idea. It may be that the cause of your problem was silently gobbled up in one of those. Other than that, I didn't look too closely at the code you posted, because it is too tedious to read unformatted code; maybe you could repost it, but this time, please use the java tags as described in the Formatting Your Post in the gray box to the right, while writing your post.

Pratheeba

Posts: 11
Nickname: pratheeba
Registered: Sep, 2003

Re: method that is not working inside thread Posted: Nov 14, 2003 5:47 PM
Reply to this message Reply
Hi,

Thanks for u'r reply...I figured it out. I took out the synchronized keyword from run() method & refresh().Now it works fine...

Flat View: This topic has 4 replies on 1 page
Topic: URL Link in JTable Previous Topic   Next Topic Topic: Script conflict ! Help !

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use