有人可以解释为什么程序没有结束吗?
Can some one explain why program doesn't end?
Hi, I'm new to the java thread.I'm trying to figure out what is the
wait() and notify() methods.So I wrote simple programme but I can't
find the reason for infinity execution.Please, can someone help me to solve this?
public class StairCase {
public static void main(String[] args) {
int[] lst = {1,2,3,4,5,6,7};
Test1 t1 = new Test1(lst);
t1.setName("Test 1 ");
Test2 t2 = new Test2(lst);
t2.setName("Test 2 ");
t1.start();
t2.start();
}
}
class Test1 extends Thread {
int[] line ;
public Test1(int[] lst) {
this.line = lst;
}
public void run(){
synchronized(line) {
for (int i = 0; i < 5; i++) {
try{
if(i == 2) line.wait();
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
}
}
class Test2 extends Thread {
int[] line ;
public Test2(int[] lst) {
this.line = lst;
}
public void run() {
synchronized(line) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
try {
line.notify();
} catch(Exception e) {
}
}
}
public class StairCase {
public static void main(String[] args) {
int[] lst = {1,2,3,4,5,6,7};
Test1 t1 = new Test1(lst);
t1.setName("Test 1 ");
Test2 t2 = new Test2(lst);
t2.setName("Test 2 ");
t1.start();
t2.start();
}
}
class Test1 extends Thread {
private final int[] line ;
Test1(int[] lst) {
this.line = lst;
}
public void run(){
synchronized(line) {
for (int i = 0; i < 5; i++) {
try{
if(i == 2) line.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
}
}
class Test2 extends Thread {
private final int[] line ;
Test2(int[] lst) {
this.line = lst;
}
public void run() {
synchronized(line) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
try {
synchronized (line) {
line.notify();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
试试这个代码 这是你期望的结果吗?如果没有,请告诉我预期结果是什么。
我添加了e.printStackTrace();到 catch 块并检查天气是否有异常,并且有一个异常,异常在 notify() 的第 45 行所以我用同步语句包围了通知。
synchronized (line) {
line.notify();
}
Hi, I'm new to the java thread.I'm trying to figure out what is the wait() and notify() methods.So I wrote simple programme but I can't find the reason for infinity execution.Please, can someone help me to solve this?
public class StairCase {
public static void main(String[] args) {
int[] lst = {1,2,3,4,5,6,7};
Test1 t1 = new Test1(lst);
t1.setName("Test 1 ");
Test2 t2 = new Test2(lst);
t2.setName("Test 2 ");
t1.start();
t2.start();
}
}
class Test1 extends Thread {
int[] line ;
public Test1(int[] lst) {
this.line = lst;
}
public void run(){
synchronized(line) {
for (int i = 0; i < 5; i++) {
try{
if(i == 2) line.wait();
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
}
}
class Test2 extends Thread {
int[] line ;
public Test2(int[] lst) {
this.line = lst;
}
public void run() {
synchronized(line) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
try {
line.notify();
} catch(Exception e) {
}
}
}
public class StairCase {
public static void main(String[] args) {
int[] lst = {1,2,3,4,5,6,7};
Test1 t1 = new Test1(lst);
t1.setName("Test 1 ");
Test2 t2 = new Test2(lst);
t2.setName("Test 2 ");
t1.start();
t2.start();
}
}
class Test1 extends Thread {
private final int[] line ;
Test1(int[] lst) {
this.line = lst;
}
public void run(){
synchronized(line) {
for (int i = 0; i < 5; i++) {
try{
if(i == 2) line.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
}
}
class Test2 extends Thread {
private final int[] line ;
Test2(int[] lst) {
this.line = lst;
}
public void run() {
synchronized(line) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + line[i]);
}
}
try {
synchronized (line) {
line.notify();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
试试这个代码 这是你期望的结果吗?如果没有,请告诉我预期结果是什么。
我添加了e.printStackTrace();到 catch 块并检查天气是否有异常,并且有一个异常,异常在 notify() 的第 45 行所以我用同步语句包围了通知。
synchronized (line) {
line.notify();
}