aspose file tools*
The moose likes Threads and Synchronization and the fly likes thread problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread problem" Watch "thread problem" New topic
Author

thread problem

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
How can I make my servlet wait until there is a definite result coming from another server. Right now i use Thread.sleep(10000) but the time the result arrive is not always consistent. How can i make sure the servlet will wait until the result finally arrive.
Any ideas n suggestions?
The part calling the runnable class in servlet
try{
DepositSender DS = new DepositSender(mnum, anum, pnum, onum, amount);
Thread t = new Thread(DS);
t.start();
Thread.sleep(10000);
int valresult = DS.getValuereturn();
int valresult2 = DS.getValuereturn2();
value[0] = valresult;
value[1] = valresult2;

}catch (Exception e) {
System.out.println("Accept failed: Deposit");
The runnable class file

private String merchantnumber, accountnumber, paymentnumber, onumber, amount, errstr;
int valreturn1, valreturn2;
//Constructor
DepositSender(String mnum, String acctnum, String paynum, String ordnum, String amt) {
merchantnumber = mnum;
accountnumber = acctnum;
paymentnumber =paynum;
onumber = ordnum;
amount = amt;
}
public void run()
{
try{
Socket socket = new Socket(hostname, 1234);
//Socket socket = new Socket(hostname, 1234);
socket.setSoTimeout(500000); //6 minutes
DataOutputStream sout=new DataOutputStream(socket.getOutputStream());
DataInputStream sin = new DataInputStream(socket.getInputStream());
System.out.println("send1");
String strCommand="COMMAND=DO\n";
String strParam1="max1=" + merchantnumber + "\n";
String strParam2="max2=" + accountnumber + "\n";
String strParam3="max3="+paymentnumber+"\n";
String strParam4="nax4="+onumber+"\n";
String strParam5="max5="+ amount +"\n";
int length = strCommand.length()+strParam1.length()+strParam2.length()+strParam3.length()+
strParam4.length()+strParam5.length();
sout.writeShort(length);
sout.writeBytes(strCommand);
sout.writeBytes(strParam1);
sout.writeBytes(strParam2);
sout.writeBytes(strParam3);
sout.writeBytes(strParam4);
sout.writeBytes(strParam5);
sout.flush();
System.out.println("send2");
int value = sin.readShort();
int value1 = sin.readShort();

valreturn1 = value;
valreturn2 = value1;
System.out.println("return value:"+value);
System.out.println("return value1:"+value1);
sout.close();
sin.close();
socket.close();
}
catch(Exception ioexp){
System.out.println("send error:"+ioexp.getMessage());
}
}
public int getValuereturn(){
return valreturn1;
}
public int getValuereturn2(){
return valreturn2;
}

Any changes that can make this better?
Magnus Andersson
Greenhorn

Joined: Mar 03, 2004
Posts: 9
You should not sleep(10000).... Instead use the wait() and notify() mechanism. Let your thread wait() until it has its values to process. Make a method taking the values as parameters, and after that notifies the waiting thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: thread problem
 
Similar Threads
Console I/O
switch statement
socket data compression problem
j_security_check, Please help.
Read files and replace Strings