日期:2014-05-20  浏览次数:20752 次

关于java线程同步的问题
我通过类newTicket里的变量state来同步三个线程,state=1时运行276: public synchronized void run_schwarz(String strcmd)这段代码,然后这段代码的末尾运行
314: state=2;
315: notifyAll(); ///!!!!!!!!!!!!!!!
来将state设为2,来运行第二段代码,但是从打印的结果来看,state始终等于1,第一个线程的调用了 p = rn.exec(schwarzexepath); 来执行一个exe,这个exe的执行结果是对的,但是为什么state没有改变呢?
求教各位高手,代码如下

20:
21:
22:public class winpcapMulti_bat extends HttpServlet
23:{
24: public void doGet(HttpServletRequest request, HttpServletResponse response) 
25: throws IOException,ServletException
26: {
27:
28:
29:
30:
31: PrintWriter out=response.getWriter();
32:
33:
34:
35: out.write("<html>\r\n");
36: out.write("<head>\r\n");
×××××××××××××
60:
61:
62: // 
63:
64: String string_port_num; 
65: BufferedReader in=new BufferedReader(new FileReader(netportlistfileName));
66: string_port_num=in.readLine();
67: in.close();
68:
69: out.write("string_port_num is "+string_port_num+"<p>");
70:
71: //
72: /********************************************************************************/
73: newTickets t=new newTickets( string_port_num,netportexepath,sendpacketexepath,schwarzexepath,filelistname,schwarzcmdfilename , schwarzbatpath );
74: out.write("the filelistsize is"+t.getFilelistsize()+"<p>");
75:
76: out.write("the state is"+t.getState()+"<p>");
77: new Producer_schwarz(t).start();
78: out.write("the state is"+t.getState()+"<p>");
79: out.write("producer schwarz already run<p>");
80:
81:  
82: new Consumer_netport(t).start();
83: out.write("the state is"+t.getState()+"<p>");
84: new Consumer_send(t).start();
85: out.write("the state is"+t.getState()+"<p>");
86: out.write("producer and two consumer already run");
87: out.write("the state is"+t.getState()+"<p>");
88:
89: /**********************************************************************************/
90:
91:
92:
93:
94:
95:
96:
97:
98:
99: out.write("</body>\r\n");
100: out.write("</html>");
101:
102:
103:
104:
105:
106:
107:
108:
109: }//fun: doget
110:}//class
111:
112:
113:class newTickets
114:
115:{
116:
117:
118:
119: int state=1; 
120: int filelistsize; 
121: String string_port_num;
122: String netportexepath;
123: String sendpacketexepath;
124: String schwarzexepath;
125: String schwarzcmdfilename;
126: String schwarzbatpath;
127:
128: String filelistname;
129:
130: public newTickets(String string_port_num, String netportexepath,String sendpacketexepath, String schwarzexepath, String filelistname,String schwarzcmdfilename,String schwarzbatpath)
131: {
132: this.string_port_num=string_port_num;
133: this.netportexepath=netportexepath;
134: this.sendpacketexepath=sendpacketexepath;
135: this.schwarzexepath=schwarzexepath;
136: this.filelistname=filelistname;
137: this.schwarzcmdfilename=schwarzcmdfilename;
138:this.schwarzbatpath=schwarzbatpath;
139: this.state=1;
140:
141: readfilelist(filelistname);/// 获得需要采集的数据文件的个数
142:
143: }//constructor
144:  
145:
146: public int getFilelistsize() /// 返回需要采集的数据文件的个数