I want to create and start 5 java threads. The threads should display message then stop execution. Am I doing this correctly or not?
public class HelloThread extends Thread { private String thread_name; // constructor HelloThread(String tname) { thread_name = new String(tname); } // override method run() public void run() { setName(thread_name); System.out.println(" Thread " + thread_name); //assigning each thread a name } public static void main(String args[]) { for (int i = 1; i < 6; i++) { HelloThread mythr_obj = new HelloThread(i + " says Hello World!!! "); mythr_obj.start(); // start execution of the thread object } } }
new String(tname), why do you want to make a copy of the string? Otherwise, are you doing it correctly? Well, does it do what you expect it to do? I mean, it's only a "Hello World" style program, there isn't much design-wise you could do wrong.