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-27 20:35:40 浏览: 12
主程序应该创建两个线程并调用Runnable实现的Communication类的run()方法,具体实现如下:
```
public class Main {
public static void main(String[] args) {
Communication communication = new Communication();
Thread thread1 = new Thread(communication);
Thread thread2 = new Thread(communication);
thread1.setName("Thread-1");
thread2.setName("Thread-2");
thread1.start();
thread2.start();
}
}
```
在主程序中,我们首先创建Communication对象,然后创建两个线程并将Communication对象作为参数传入线程的构造函数中,最后启动这两个线程。由于两个线程共享Communication对象,因此它们将交替输出数字1到100。其中,notify()方法用于唤醒等待中的线程,wait()方法用于使线程等待,synchronized关键字用于实现线程同步。
相关问题
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) { }}}}}
这段代码是一个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();
}
}
}
}
```