0

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 } } } 
6
  • 1
    There's no need for 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. Commented Sep 9, 2015 at 13:32
  • If this code actually compiles, I would recommend asking on codereview.stackexchange.com, as the possible answers are probably too broad/opinion based for here. Commented Sep 9, 2015 at 13:32
  • @AndyBrown This isn't really on topic for Code Review as it looks like the post just contains example code. Even if that compiles and runs it's off topic as CR is for reviewing real code. Commented Sep 9, 2015 at 13:34
  • @SuperBiasedMan - fair enough, although we now know what the real question is: "I'm not sure if its creating multiple threads or just creating one at a time then stopping", so the nature of this has changed somewhat. Commented Sep 9, 2015 at 13:37
  • 2
    @OldCurmudgeon - they aren't daemon threads, so the JVM will wait until they complete and the printlns will be seen. Commented Sep 9, 2015 at 13:43

3 Answers 3

2

Since the introduction of the java.util.concurrent libraries in java 1.4, developers rarely create their own Thread instances these days.

Today, you're more likely to do

ExecutorService threadPool = Executors.newFixedThreadPool(5); List<Future<Integer>> futures = new ArrayList<>(); for (int i = 0; i < 20; ++ i) { Callable<Integer> callable = () -> { TimeUnit.SECONDS.sleep(1); System.out.println("Returning " + i); return i; }; Future<Integer> future = threadPool.submit(callable); futures.add(future); } for (Future<Integer> future : futures) { Integer result = future.get(); System.out.println("Finished " + result); } threadPool.shutdown(); 
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried compiling and running this code? It looks correct, although I would recommend having your main method in a separate class.

4 Comments

Yes it works but I was just curious about starting the threads in the loop. I'm not sure if its creating multiple threads or just creating one at a time then stopping.
@golferdrew32 - I suggest you ask that exact question instead of a question that doesn't state what you want to know.
@golferdrew32: Whenever start() is called, a new thread spins off and the old thread continues on. What you're doing is an entirely valid, and in fact fairly standard, way of starting threads. Try putting something like sleep(Random.getInt()) in your run() method to see this more visibly illustrated.
@golferdrew32 It is creating multiple threads, that can easily be seen if you add System.out.println("Thread started"); inside that loop, and see the order the messages are printed in.
0

Yes. You did it well but as @FSQ suggested, your whole class is itself a thread. You may put the main method in any other class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.