我在课堂上有2个功能:
login()
登录网站
Send_Sms()
发送短信
这里我想为函数添加2个线程,以便可以在Send_Sms
成功完成后开始发送短信的方式控制函数login()
的优先级,否则程序无效。
这是我到目前为止所做的:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RunThreads extends Thread
{
public void Login(String username,String password)
{
try
{
// creates the batch file for Logging into abc.com server
Writer output = null;
String UserAgent = "Mozilla/5.0 (Windows NT 5.1;KM:10.0.2)";
String postdata = "\"" + "username="+username+ "&password=" + password + "&button=Login" + "\"";
String MainStream = "c:\\wget.exe --output-document=login.html --user-agent=" + "\"" + UserAgent + "\"" + " --max-redirect=10 --cookies=on --keep-session-cookies --save-cookies=cookie.txt --post-data " + postdata + " http://www.abc.com/Login1.action;";
File file = new File("login.bat");
output = new BufferedWriter(new FileWriter(file));
output.write(MainStream);
output.close();
Runtime.getRuntime().exec("cmd /c start login.bat");
}
catch (IOException ex)
{
Logger.getLogger(Save_Credentials.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void SendSms(String mobno,String content) throws IOException
{
Writer output = null;
String UserAgent = "Mozilla/5.0 (Windows NT 5.1;KM:10.0.2)";
String MainStream = "c:\\wget.exe --output-document=quicksms.html --user-agent=" + "\"" + UserAgent + "\"" + " --referer=http://www.abc.com/jsp/SMS.jsp --cookies=on --keep-session-cookies --load-cookies=cookie.txt --save-cookies=cookie.txt --post-data "+"\""+"&HiddenAction=instantsms&catnamedis=Birthday&Action=gstahsbdf5346g&chkall=on&MobNo="+mobno+"&textArea="+content+"\""+" http://www.abc.com/quicksms.action;";
File file = new File("SendSms.bat");
output = new BufferedWriter(new FileWriter(file));
output.write(MainStream);
output.close();
Runtime.getRuntime().exec("cmd /c start SendSms.bat");
}
public static void main(String[] args)
{
}
}
答案 0 :(得分:0)
由于登录失败时无法发送短信,正如您自己所说:
可以以某种方式控制Send_Sms功能的优化 它在login()完成后开始发送短信 成功否则程序将无法正常工作。
然后这两个任务是顺序耦合的,这意味着在不同的线程上并行执行它们是没有意义的。只需在一个线程上按顺序执行它们。