new Thread(new Runnable() { @Override public void run() { System.out.println("Hello World!"); } });将此段的代码用Lambda表达式
时间: 2024-02-13 11:01:08 浏览: 20
可以将上述代码改写为Lambda表达式如下:
```
new Thread(() -> System.out.println("Hello World!"));
```
Lambda表达式可以省略掉匿名内部类中的一些冗余代码,只需要提供需要实现的方法即可。在这个例子中,我们使用箭头符号 -> 来指定要实现的方法,即 run() 方法。需要注意的是,这里 run() 方法没有显式地写出来,而是由编译器根据上下文自动推断出来的。
相关问题
public class ThreadHomework { public static void main(String[] args) { T1 t1 = new T1(); T2 t2 = new T2(); 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++) { System.out.println("hello,world"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class T2 implements Runnable { @Override public void run() { for (int i = 1; i <= 5; i++) { System.out.println("hi"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } 修改代码使其输出为hi 1 hello,world 1 hi 2 hello,world 2 hi 3 hello,world 3 hi 4 hello,world 4 hi 5 hello,world 5 hello,world 6 hello,world 7 hello,world 8 hello,world 9 hello,world 10
以下是修改后的Java程序代码:
```
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();
}
}
}
}
```
在上面的代码中,我们对T1和T2类进行了修改,以实现按照指定的输出顺序输出。具体修改如下:
- 在T1类中,我们使用synchronized关键字对输出语句进行了加锁,并使用wait()和notify()方法来控制线程的执行顺序。在每次输出后,我们调用notify()方法唤醒T2线程并让T1线程进入等待状态,直到T2线程输出完毕后再唤醒T1线程继续执行。
- 在T2类中,我们添加了一个T1类型的成员变量t1,并在构造方法中初始化。在run()方法中,我们使用synchronized关键字对t1对象进行加锁,并使用wait()和notify()方法来控制线程的执行顺序。在每次输出后,我们调用notify()方法唤醒T1线程并让T2线程进入等待状态,直到T1线程输出完毕后再唤醒T2线程继续执行。
当程序运行时,将会按照指定的顺序输出“hi”和“hello,world”,直到输出完毕后两个线程将会自动退出。
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();
}
}
}
}
```