English 中文(简体)
建筑物内的新铺面
原标题:calling new thread inside is constructor

是否应当像这里那样,在班级建筑商内建立一线并称之为开端方法?

public class Server implements Runnable {

    private ServerSocket server;

    public Server(int port) {
        try {
            //Opens a new server 
            server = new ServerSocket(port);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        new Thread(this, "Server").start();
    }

    @Override
    public void run() {
    }
}
最佳回答

IMHO不这样做。 页: 1

问题回答

你们的法典是这样做的,但是如果你们的法典认为:

public Server(int port)
{       
    new Thread(this, "Server").start();

    try
    {
        //Opens a new server 
        server = new ServerSocket(port);
    }
    catch (IOException ioe){ ioe.printStackTrace(); }

}
@Override    
public void run(){
    if(server == null)throw new NullPointerException();// this may happen
}
}

The server reference may be null even though no exception occurs. This is because the Thread will use the created runnable and invoke the run method even if the constructor of your class hasn t finished.

Server s = new Server();
Thread t = new Thread(s, "Server").start();

更可检测。 这使你能够创建服务器和单位测试其方法而不播下透镜。

A couple more good reasons to split the Thread.start() from the constructor:

  1. If you ever want to use some other framework/system to run the threads, such as a java.util.concurrent.Executor, you may do so.
  2. 如果你想要打断read,你需要提及。 在不同的法典系中建立校对,使得这一条更加例行。 e.g.

    阅读 Me = 新版(服务器);

在您的原法典中,服务器可以有一个领域来缅怀我的座右铭,但它的确如此。

public class Server implements Runnable
{
private ServerSocket server;

/**
 * Because the constructor is private, the only way to instantiate a Server is through
 * the static factory method.
 * If there are any instantiation problems, the static factory method will fail in
 * first line, before it is put into a thread.
 * It will be put into a thread before being released.
 **/    
public static Server startServer ( int port )
{
    Server server = new Server ( port ) ;
    new Thread ( server , "Server" ) . start ( ) ;
    return server ;
}

private Server(int port)
{       
    try
    {
        //Opens a new server 
        server = new ServerSocket(port);
    }
    catch (IOException ioe){ ioe.printStackTrace(); }

//    don t release me into the wild yet!
//    new Thread(this, "Server").start();
}
@Override    
public void run(){
}
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签