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();
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With