class Communication implements Runnable{ int i = 1; public void run() { while (true) { synchronized (this) { notify(); if (i <= 100) { System.out.println(Thread.currentThread().getName() + ":" + i++); } else break; try { wait();} catch (InterruptedException e) { }}}}}
时间: 2024-03-29 17:37:01 浏览: 13
这段代码是一个Java多线程的示例,其中定义了一个实现了Runnable接口的Communication类,它包含一个整型变量i和一个无限循环的while语句。在while循环中,使用了synchronized关键字来保证同步,notify()方法和wait()方法来实现线程的通信。在每次循环中,线程会先调用notify()方法来唤醒其他线程,然后判断i的值是否小于等于100,如果是,则输出当前线程的名称和i的值,然后i自增1。最后,线程调用wait()方法进入等待状态,直到其他线程调用notify()方法唤醒它。当i的值大于100时,线程跳出循环,执行完毕。
相关问题
public class ThreadHomework { public static void main(String[] args) { T1 t1 = new T1(); T2 t2 = new T2(t1); Thread th1 = new Thread(t1); Thread th2 = new Thread(t2); th1.start(); th2.start(); } } class T1 implements Runnable { @Override public void run() { for (int i = 1; i <= 10; i++) { synchronized (this) { System.out.println("hello,world " + i); notify(); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } synchronized (this) { notify(); } } } class T2 implements Runnable { private T1 t1; public T2(T1 t1) { this.t1 = t1; } @Override public void run() { for (int i = 1; i <= 5; i++) { synchronized (t1) { try { t1.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("hi " + i); t1.notify(); } } } } 编译错误. java:5:错误:类T2中的构造函数T2不能应用于给定的类型; T2 T2=新的T2(); ^ 要求:T1 找到:没有参数 原因:实际论证列表和形式论证列表的长度不同 1个错误, 进一步修改代码
根据错误提示,我们需要在创建 T2 对象时传入一个 T1 类型的参数。因此,需要修改下面这行代码:
```
T2 t2 = new T2(t1);
```
这里已经正确地传入了 T1 类型的参数 t1,所以不需要再创建一个新的 T1 对象。因此,可以将下面这行代码删除:
```
T1 t1 = new T1();
```
最终的代码如下所示:
```
public class ThreadHomework {
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2(t1);
Thread th1 = new Thread(t1);
Thread th2 = new Thread(t2);
th1.start();
th2.start();
}
}
class T1 implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
synchronized (this) {
System.out.println("hello,world " + i);
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (this) {
notify();
}
}
}
class T2 implements Runnable {
private T1 t1;
public T2(T1 t1) {
this.t1 = t1;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
synchronized (t1) {
try {
t1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hi " + i);
t1.notify();
}
}
}
}
```
public class E { public static void main(String[] args) { Target t = new Target(); Thread dog = new Thread(t); Thread cat = new Thread(t); dog.start(); cat.start(); } } class Target implements Runnable { int number = 0; public void run() { fo(); } public synchronized void fo() { while (number < 6) { try { number = 6; wait(); System.out.printf("%d", number); } catch (InterruptedException exp) { } } number = 10; notify(); } }
这段代码的作用是创建两个线程(dog和cat),这两个线程都使用同一个Target对象t作为Runnable,然后启动这两个线程。在Target类中,有一个fo()方法,该方法使用synchronized关键字进行同步,保证了多线程之间的安全性。在fo()方法中,使用while循环判断number是否小于6,如果小于6,则将number设置为6并使用wait()方法让当前线程等待。当另一个线程调用notify()方法时,该线程被唤醒并输出number的值(此时number为6)。当while循环结束后,将number设置为10并使用notify()方法唤醒另一个线程。
然而,这段代码存在一些问题。首先,while循环中的number=6应该放在wait()方法之前,否则线程会一直等待。其次,notify()方法应该在修改number值之后调用,否则另一个线程可能会在修改number值之前被唤醒。最后,fo()方法的实现比较奇怪,number的值只会被修改一次,并且while循环中的条件永远不会变化,因此这个程序实际上只会输出一次数字6。