Multithreading Java:启动线程的方法
我在下面找到了构建简单Java聊天室的基础知识: 基本上,ChatClient使用以下代码启动ChatClientThread:Multithreading Java:启动线程的方法,multithreading,Multithreading,我在下面找到了构建简单Java聊天室的基础知识: 基本上,ChatClient使用以下代码启动ChatClientThread: if (thread == null) { client = new ChatClientThread(this, socket); thread = new Thread(this); thread.start(); } 有人能帮我理解上述代码与此代码之间的区别吗: if (thread ==
if (thread == null)
{ client = new ChatClientThread(this, socket);
thread = new Thread(this);
thread.start();
}
有人能帮我理解上述代码与此代码之间的区别吗:
if (thread == null)
{ client = new ChatClientThread(this, socket);
client.start();
}
是否仅用于设置线程!=空还是有具体原因?在这种情况下,“客户机”线程是如何传递给带有“this”一词的新线程的?
感谢您的帮助。总结
if(thread == null)
{
client = new ChatClientThread(this, socket);
thread = new Thread(this);
thread.start();
}
if(thread == null)
{
client = new ChatClientThread(this, socket);
client.start();
}
Here you need to aware about the following things
first "this" => this is keyword in java which returns running instance of class
second "ChatClientThread" => ChatClientThread declared as follows
"public class ChatClientThread extends Thread " that means ChatClientThread is child class of Thread class.
So in first block we are passing Runnable object(this) to the ChatClientThread which will then pass to thread,=> client = new ChatClientThread(this, socket);
and in next statement we are passing Runnable object(this) to the thread itself.=>
thread = new Thread(this);
So in first block we are having two runnable instance that can be run
and in second we have only one ie. customized ChatClientThread
简单的回答是,带有thread.start()的线程创建了2个线程,带有client.start()的线程创建了1个线程,其中可能抛出了IllegalThreadStateException
为了充分解释这个场景,我将把问题分为三个部分;thread.start()代码、client.start()代码和ChatClientThread实例化
第1部分——thread.start() 在此代码块中,执行以下步骤:
线程
是否为null
client
变量实例化为ChatClientThread
的新实例,将当前的ChatClient
作为其客户端,将socket
作为其套接字Runnable
时,该对象必须重写public void run()
。线程
类实现将执行提供的Runnable的run()
方法ChatClient
各自的run()
方法,因为我们将线程实例化为runnable
线程.start()执行的
第2部分--客户端.start()
在此代码块中,执行以下步骤:
检查线程
是否为null
将client
变量实例化为ChatClientThread
的新实例,将当前的ChatClient
作为其客户端,将socket
作为其套接字
启动ChatClientThread
。这将执行ChatClientThread
的run()
方法
下面是将由客户端.start()执行的代码。
:
第3部分——ChatClientThread实例化
现在,ChatClientThread构造函数包含一些需要注意的重要代码
public ChatClientThread(ChatClient _client, Socket _socket)
{
client = _client;
socket = _socket;
open();
start();
}
在此代码块中,执行以下步骤:
初始化客户端
变量
初始化套接字
变量
执行open()
函数。此函数创建将用于接收输入的DataInputSteam对象
执行start()
函数。此函数将启动ChatClientThread
的此实例,并执行其相应的run()
方法。请注意,ChatClientThread
的当前状态此时正在运行。如果我们重新阅读第2部分的代码,我们将看到在客户机变量客户机.start()初始化后执行。由于线程被启动了两次,这可能会抛出一个IllegalThreadStateException
。一旦启动,就不能在线程上再次调用start
下面是要由start()
执行的命令:
结论
在第1部分中,应用程序将创建2个线程;一个是运行自己的run()
方法的ChatClientThread
,另一个是运行ChatClient
的run()
方法的thread
变量
在第2部分中,应用程序将创建1个线程;运行自己的run()
方法的ChatClientThread
。client.start()
函数可能会抛出一个IllegalThreadStateException
,因为ChatClientThread
将启动两次。在第一个版本中,您将启动一个线程,该线程将在“this”上调用run,无论“this”是什么。第二个版本,启动ChatClientThread。这将导致调用其run方法。正如@matt所回答的,区别实际上是您想要如何控制它。第二个例子看起来不那么准确,因为如果client.start()
调用一个新线程,那么thread==null
检查实际上并不控制该线程引用,这仍然是一个假设,因为您尚未公布哪个线程持有线程
引用。
public void run()
{
while (true)
{
try
{
streamOut.writeUTF(console.readLine());
streamOut.flush();
}
catch(IOException ioe)
{
System.out.println("Sending error: " + ioe.getMessage());
stop();
}
}
}
if (thread == null)
{
client = new ChatClientThread(this, socket);
client.start();
}
public void run()
{
while (true)
{
try
{
client.handle(streamIn.readUTF());
}
catch(IOException ioe)
{
System.out.println("Listening error: " + ioe.getMessage());
client.stop();
}
}
}
public ChatClientThread(ChatClient _client, Socket _socket)
{
client = _client;
socket = _socket;
open();
start();
}
public void run() {
while (true)
{
try
{
client.handle(streamIn.readUTF());
}
catch(IOException ioe)
{
System.out.println("Listening error: " + ioe.getMessage());
client.stop();
}
}
}