gpt4 book ai didi

java - Java中的多线程编程

转载 作者:行者123 更新时间:2023-11-29 08:46:26 25 4
gpt4 key购买 nike

我正在学习 Java 中的多线程概念,并遇到了一个通过实现可运行类来创建新线程的程序。这是我有疑问的部分代码:

class Demo implements Runnable
{
Thread t;
Demo()
{
t=new Thread(this,"child_thread1");
System.out.println("Thread Info:"+t);
t.start();
}
}

现在,谁能给我解释一下this有什么用?我没有传递 this,而是尝试传递 Demo 类的对象。原来 child_thread1 甚至没有运行!

最佳答案

this”:-

this keyword can be used to refer current class instance variable.

this() can be used to invoke current class constructor.

this keyword can be used to invoke current class method (implicitly).

this can be passed as an argument in the method call.

this can be passed as argument in the constructor call.

this keyword can also be used to return the current class instance.

关于java - Java中的多线程编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25092615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com