我需要幫助,我嘗試使用同步線程Java編寫,但它不能運行......線程同步無法工作
我有兩個班,像這樣
for(int j=0;j<idW.length;j++){
webtext = d.getWebText(idW[j]);
ThreadPrepo tpo =new ThreadPrepo(webtext, host[j%jumhost], "server", 1099,idW[j]);
Thread t1=new Thread(tpo);
t1.start();
}
//thread untuk setfitur tanpa rmi
int ukuran=idW.length;
ProsesSetfitur pro=new ProsesSetfitur(idW);
Thread t2=new Thread(pro);
t2.start();
這個線程是在類threadprepo的代碼:
public class ThreadPrepo implements Runnable{
String host,server,c,webtext;
int port,idweb;
DataDB db=new DataDB();
public ThreadPrepo(String webtext,String host,String server,int port,int idweb){
this.webtext=webtext;
this.host=host;
this.server=server;
this.port=port;
this.idweb=idweb;
}
@Override
public void run(){
preponi();
}
public synchronized void preponi(){
try{
System.out.println("hostnya :"+host);
Registry reg=LocateRegistry.getRegistry(host,port);
Sportrmijob rmi=(Sportrmijob) reg.lookup("server");
rmi.SetInput(webtext);
List l=rmi.getresult();
String[] hasilprep=new String[l.size()];
for(int k=0;k<l.size();k++){
hasilprep[k]=l.get(k).toString();
}
db.insertWordney(idweb, hasilprep);
String [][] frekdb=db.getFrekDB(idweb);
db.doinsertfrek(idweb,frekdb);
}
catch(Exception e){
System.out.println("error di class threadprepo "+e.getMessage());
}
}
}
,然後這是類prosesSetFitur代碼
public class ProsesSetfitur implements Runnable{
DataDB d=new DataDB();
int []idweb;
public ProsesSetfitur(int[]idweb){
this.idweb=idweb;
}
@Override
public void run(){
try{
Thread.sleep(500);
setfitur();
}
catch(Exception e){
System.out.println("error setfitur "+e.getMessage());
}
}
public synchronized void setfitur() throws InterruptedException{
System.out.println("(proses setfitur)");
String []allkata;
String fitur;
String []fiturs=new String[15];
String []kata_kata=new String[15];
System.out.println("nilai iD="+idweb.length);
for(int s=0;s<idweb.length;s++){
//System.out.println("IDWEEEEEEEEEEB"+idweb[s]);
allkata=d.getUrutanKata(idweb[s]);
for(int u=0;u<15;u++){
// System.out.println("PERULANGAN U KE"+u);
if(u<=4){
fitur="T";
//System.out.println("kata ke" +u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>4&&u<10){
fitur="S";
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
else if(u>=10&&u<15){
fitur="R" ;
//System.out.println("kata ke"+u+" = "+allkata[u]+" fiturnya = "+fitur);
kata_kata[u]=allkata[u];
fiturs[u]=fitur;
}
}
d.insertfitur(idweb[s], kata_kata, fiturs);
}
}
誰能給我的解決方案來解決這個問題...爲什麼螺紋類ProsesSetFitur是執行第一?同步線程是如何工作的?請幫助...
確定...感謝ü@java尋你的答案..我更加明白... 你有解決方案來解決這個問題,讓課堂ThreadPrepo線程可以先執行和那麼之後執行ProsesSetFitur類中的線程? – zorro
...我已經嘗試刪除修改在同步preponi()和setfitur(),但在類ProsesSetFitur仍然線程首先被執行......請幫我T^T ... – zorro
看到我的編輯答案 –