Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java understand that threads which have same name are different threads?

In each iteration, a thread "student" is created. Because those threads are all named "student". Can Java understand that these are different thread?

while (true) {
    System.out.println("Waiting for client...");
    // open client socket to accept connection
    client = server.accept();
    System.out.println(client.getInetAddress()+" contacted ");
    System.out.println("Creating thread to serve request");

    ServerStudentThread student = new ServerStudentThread(client);
    student.start();
}
like image 288
John Avatar asked Feb 12 '11 12:02

John


People also ask

Is Java thread name unique?

The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime.

Can multiple threads write to the same file Java?

You have to append to the existing file ( FileOutputStream has a boolean flag for that). But you would also have to make the writing synchronized , to make sure only one Thread writes at the same time. Don't use StringBuffer if you can use StringBuilder. In this case processId() can return a String.

Can multiple threads read the same object?

Objects are not automatically copied. You need to take care that multiple threads are not modifying the same object simultaneously. See this tutorial about concurrency, for example.


1 Answers

From the Javadoc:

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

The JVM tracks threads by their ID, not by their name.

like image 197
skaffman Avatar answered Oct 08 '22 17:10

skaffman