1. private 改成public2. exampe 大写 构造函数里面调用super()
3.private static int getCount() {
return count;
} static去掉4.getCount() = 0 改成 getCount() == 0
5.加上out.flush(); out.cloas();6.d.length() 改d.length
3.private static int getCount() {
return count;
} static去掉4.getCount() = 0 改成 getCount() == 0
5.加上out.flush(); out.cloas();6.d.length() 改d.length
解决方案 »
- 求助,做了一个酒店管理,现在卡在统计上了,各路高手快请帮帮忙!!
- Hibernate update(obj)异常 Unexpected row count: 137 expected: 1
- javaBean调用EJB(用的JBoss应用服务器,EJB3.0)
- [折扣] > 10 and [优惠] > 20 怎么取出所有[]中的所有数据啊
- 求高手们告诉下解密算法,这几天等着急即用啊
- Jboss启动出错
- 请教通过web.xml引入Spring上下文的问题?
- FileForm中上传中文文件,文件名出现乱码
- JSP 的text框小技巧
- 请教关于审计与跟踪的设计
- EJB3.0,存储过程返回部分字段,装配成对象时报错。
- 面试题目
只能声明为public,abstract,final的2.private static int getCount() {
return count;
} static去掉静态方法不能调用非静态变量
3.getCount() = 0 改成 getCount() == 0
4.加上out.flush(); out.cloas();5.d.length() 改d.length
1、private class example extends HttpServlet{
pbulic class example extends HttpServlet{2、int count;
int count=0;3、private static int getCount(){
private int getCount(){4、if(getCount() = 0 )
if(getCount() == 0 )5、out.println( "length of the array d = " + d.length() );
out.println( "length of the array d = " + d.length );线程那道还没教,不予回答,哈哈
//To create default constructor
public void example() {
++count;
}
这里的注释说的是创建默认构造函数,而且如果此方法是普通方法的话,在main函数里也没调用,所以这里应该是写的构造函数,构造函数没有返回值,方法名必须与类名一致,而且在默认构造函数里必须对类成员变量赋值。
SMTPReceiver sr = new SMTPReceiver();
Thread worker = new Thread(sr);
worker.start();
// 暂停
while(sr.m_IsWorking){
worker.interrupt();
Thread.sleep(1000);
}
// 恢复
while(!sr.m_IsWorking){
worker.interrupt();
Thread.sleep(1000);
}
// 退出
Field field = SMTPReceiver.class.getDeclaredField("m_stopApp");
field.setAccessible(true);
field.setBoolean(sr, true);
field.setAccessible(false);
System.out.println(sr.getStopFlag());
}
SMTPReceiver sr = new SMTPReceiver();
Thread worker = new Thread(sr);
worker.start();
// 暂停
while(sr.m_IsWorking){
worker.interrupt();
Thread.sleep(1000);
}
// 恢复
while(!sr.m_IsWorking){
worker.interrupt();
Thread.sleep(1000);
}
// 退出
Field field = SMTPReceiver.class.getDeclaredField("m_stopApp");
field.setAccessible(true);
field.setBoolean(sr, true);
field.setAccessible(false);
}
pbulic class example extends HttpServlet{2、exampe 类和构造方法大写3、private static int getCount(){
private int getCount(){4、if(getCount() = 0 )
if(getCount() == 0 )5、out.println( "length of the array d = " + d.length() );
out.println( "length of the array d = " + d.length );
怎么没人说把构造函数的void 去掉的??
第二题的错误大家说过的我就不说了,只是有一点,
//To create default constructor
public void example() {
++count;
}
这里的注释说的是创建默认构造函数,而且如果此方法是普通方法的话,在main函数里也没调用,所以这里应该是写的构造函数,构造函数没有返回值,方法名必须与类名一致,而且在默认构造函数里必须对类成员变量赋值。
public boolean m_IsWorking = true; // true为线程在执行状态,false为暂停 private boolean m_stopApp = false; // true表示线程状态live; false为dead public void run() {
while (true) {
if (this.m_stopApp)
break; m_IsWorking = true;
// 在此执行大数据量文件操作 try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
m_IsWorking = false; // 线程暂停
try {
while (true) {
Thread.sleep(1000);
}
} catch (InterruptedException iie) {
}
}
}
} public static void main(String[] args) throws Exception {
SMTPReceiver sr = new SMTPReceiver();
Thread worker = new Thread(sr);
worker.start();
// ***************
// 暂停线程
// 先暂停后恢复线程
// 退出线程
// 重启线程
// 暂停
while (sr.m_IsWorking) {
worker.interrupt();
Thread.sleep(1000);
} // 恢复
while (!sr.m_IsWorking) {
worker.interrupt();
Thread.sleep(1000);
} // 退出
Field field = SMTPReceiver.class.getDeclaredField("m_stopApp");
field.setAccessible(true);
field.setBoolean(sr, true);
field.setAccessible(false);
// System.out.println(sr.getStopFlag());
// 重启线程??? }
}
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.Date;public class SMTPReceiver implements Runnable {
public boolean m_IsWorking = true; // true为线程在执行状态,false为暂停
private boolean m_stopApp = false; // true表示线程状态live; false为dead public void run() {
System.out.println("Just start running at :" + new Date());
while (true) {
if (this.m_stopApp) {
System.out.println("Receive exit order,about to exit.");
break;
} m_IsWorking = true;
// 在此执行大数据量文件操作 try {
// Simulate the file operations
// System.out.println("Simulating working...");
Thread.sleep(1000);
} catch (InterruptedException ie) {
// if interrupted when operating the big
// amount of files wait until interrupted
// to run again
System.out.println("Receiving pause order,pauing.");
m_IsWorking = false; // 线程暂停
try {
while (true) {
Thread.sleep(200000);
}
} catch (InterruptedException iie) {
System.out.println("Wake up from the paused state.");
}
}
}
} public static void main(String[] args) throws Exception {
SMTPReceiver sr = new SMTPReceiver();
Thread worker = new Thread(sr);
worker.start();
// ***************
boolean shouldExit = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (!shouldExit) {
System.out.print("Please input command 1/pause 2/stop 3/restart 4/wakeup 5/exit):");
String commandString = br.readLine();
int command = -1;
try {
command = Integer.parseInt(commandString);
} catch (Exception e) {
// e.printStackTrace();
}
switch (command) {
case 1 :
// pause
if (sr.m_IsWorking) {
worker.interrupt();
}
break;
case 2 :
// stop
Field shouldStopField = SMTPReceiver.class.getDeclaredField("m_stopApp");
shouldStopField.setBoolean(sr, true);
while (worker.isAlive()) {
System.out.println("Wait for the old worker to die.");
}
if (!sr.m_IsWorking) {
worker.interrupt();
}
break;
case 3 :
// restart
// stop
shouldStopField = SMTPReceiver.class.getDeclaredField("m_stopApp");
shouldStopField.setBoolean(sr, true);
while (worker.isAlive()) {
System.out.println("Wait for the old worker to die.");
}
shouldStopField.setBoolean(sr, false);
// start
worker = new Thread(sr);
worker.start();
break;
case 4 :
// wakeup
if (!sr.m_IsWorking) {
worker.interrupt();
}
break;
case 5 :
shouldExit = true;
break;
default :
System.out.println();
System.out.println("Invalid command.");
}
} }
}
先停止了再start?
还是暂停在恢复叫重启