Java 中断异常;必须被抓住或宣布被抛出
我不断得到以下错误:Java 中断异常;必须被抓住或宣布被抛出,java,exception,thread-sleep,java-threads,Java,Exception,Thread Sleep,Java Threads,我不断得到以下错误: PrintServerV1.java:63: error: unreported exception InterruptedException; must be caught or declared to be thrown server.printRequest("homework7.txt"); ^ PrintServerV1.java:64: error: unreported exception
PrintServerV1.java:63: error: unreported exception InterruptedException; must be caught or declared to be thrown
server.printRequest("homework7.txt");
^
PrintServerV1.java:64: error: unreported exception InterruptedException; must be caught or declared to be thrown
server.printRequest("assignment4.txt");
^
PrintServerV1.java:65: error: unreported exception InterruptedException; must be caught or declared to be thrown
server.printRequest("speech.pdf");
我确信我已经抛出了错误。此错误仅在添加以下行时发生:Thread.sleep(1000)
这是我的密码:
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
import static java.lang.System.out;
public class PrintServerV1 implements Runnable {
private static final Queue<String> requests = new LinkedList<String>();
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void printRequest(String s) throws InterruptedException {
lock.lock();
try {
out.println("Adding print request for: " + s);
requests.add(s);
condition.signal();
} finally { lock.unlock(); Thread.sleep(1000);}
}
public void sendRequest() throws InterruptedException {
lock.lock();
try {
while (requests.size() == 0) {
condition.await();
}
out.println("Sending Request to printer");
Thread.sleep(1000);
while (!requests.isEmpty()) {
realPrint(requests.remove());
}
} finally {
lock.unlock();
}
}
private void realPrint(String s) throws InterruptedException {
// do the real work of outputting the string to the screen
out.println("Currently printing: " + s);
Thread.sleep(1000);
}
public void run() {
try {
sendRequest();
} catch (InterruptedException exception) {}
}
public static void main(String[] args) {
PrintServerV1 server = new PrintServerV1();
new Thread(server).start();
server.printRequest("homework7.txt");
server.printRequest("assignment4.txt");
server.printRequest("speech.pdf");
}
}
import java.util.*;
导入java.util.concurrent.locks.Lock;
导入java.util.concurrent.locks.ReentrantLock;
导入java.util.concurrent.locks.Condition;
导入静态java.lang.System.out;
公共类PrintServerV1实现可运行{
私有静态最终队列请求=新建LinkedList();
private Lock=new ReentrantLock();
私有条件Condition=lock.newCondition();
public void printRequest(字符串s)引发InterruptedException{
lock.lock();
试一试{
out.println(“为“+s”添加打印请求);
请求。添加(s);
条件。信号();
}最后{lock.unlock();Thread.sleep(1000);}
}
public void sendRequest()引发InterruptedException{
lock.lock();
试一试{
while(requests.size()==0){
条件wait();
}
out.println(“向打印机发送请求”);
睡眠(1000);
而(!requests.isEmpty()){
realPrint(requests.remove());
}
}最后{
lock.unlock();
}
}
私有void realPrint(字符串s)引发InterruptedException{
//完成将字符串输出到屏幕的实际工作
out.println(“当前打印:+s”);
睡眠(1000);
}
公开募捐{
试一试{
sendRequest();
}catch(InterruptedException异常){}
}
公共静态void main(字符串[]args){
PrintServerV1服务器=新的PrintServerV1();
新线程(server.start();
printRequest(“homework7.txt”);
printRequest(“assignment4.txt”);
打印请求(“speech.pdf”);
}
}
因为printRequest方法抛出InterruptedException,所以它必须在main中捕获,或者由main抛出。。。下列任一答案都应该有效
public static void main(String[] args) throws InterruptedException {
PrintServerV1 server = new PrintServerV1();
new Thread(server).start();
server.printRequest("homework7.txt"); // these throw an exception
server.printRequest("assignment4.txt"); // That is "InterruptedException"
server.printRequest("speech.pdf"); // It must be caught, or declared thrown!
// Version 1 declares it thrown (above in method declaration)
}
public static void main(String[] args) {
PrintServerV1 server = new PrintServerV1();
try {
new Thread(server).start();
server.printRequest("homework7.txt");
server.printRequest("assignment4.txt");
server.printRequest("speech.pdf");
catch (InterruptedException e) { e.printStackTrace(); } // Version 2 catches it
}
这些是编译器错误,而不是运行时错误。没有引发异常,因为程序实际上无法执行。它告诉您,您的
printRequest
被声明为抛出InterruptedException
,但在调用printRequest
的代码中没有捕捉到它。这在任何异常教程的介绍中都有很好的解释。请看一看