Help make the task with the threads, in which you need to enter the size of the array, enter the name of the values ​​in this array. I'm still new.
That is the result should be

enter image description here

Code that is already

public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Количество потоков: "); int s = in.nextInt(); try { Thread.sleep(1000); } catch(InterruptedException e) { System.out.println("Главный поток прерван"); } for (int i = s; i >0;i--){ Thread t = Thread.currentThread(); System.out.println("Создан поток " + i + " " + t); t.setName(""+i); System.out.println(t.getName() + " Завершил свою работу"); } } 

Closed due to the fact that the essence of the question is not clear to the participants by cheops , zRrr , insolor , D-side , Nick Volynkin 9 May '16 at 3:21 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Please clarify with what specifically you have a problem when writing code. Here you can get advice on a specific problem, but do not write / add code. - Risto

1 answer 1

Here is the code you need.

 public void threadEngine() { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { System.out.print("Size of pool: "); int size = new Integer(reader.readLine()); Thread[] arrayOfThread = new Thread[size]; for (int x = 0; x < size; x++) { System.out.print("New Thread Name: "); arrayOfThread[x] = new Thread(this, reader.readLine()); } for (Thread t : arrayOfThread) { t.start(); } } catch (IOException e) { System.out.println("IO Exception" + e); } } public void run() { for (int x = 0; x < 5; x++) { System.out.println(Thread.currentThread().getName() + " : " + x); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " finished"); } 

Your code is missing a call to the .start () method without which nothing is actually generated. I do not observe the overridden run method ...

I recommend reading off of the Oracle Thread documentation.

  • Yes, everything works ... I think you did not call this method from the main method (something like this) public static void main (String [] args) {new TestClass (). ThreadEngine (); } - Peter Slusar