Multithreading Java:启动线程的方法

Multithreading Java:启动线程的方法,multithreading,Multithreading,我在下面找到了构建简单Java聊天室的基础知识: 基本上,ChatClient使用以下代码启动ChatClientThread: if (thread == null) { client = new ChatClientThread(this, socket); thread = new Thread(this); thread.start(); } 有人能帮我理解上述代码与此代码之间的区别吗: if (thread ==

我在下面找到了构建简单Java聊天室的基础知识:

基本上,ChatClient使用以下代码启动ChatClientThread:

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();
            }
        }
    }