현재의 thread에 새로운 스레드에 대한 thread.start () 메소드는 즉시 반환합니까?

프라의 bhashwara 필드 :

나는 케이 S. HORSTMANN에 의해 도서 코어 자바를 읽고 있어요. 나는 섹션 동시성이 코드와 혼동하고있다. 우리는 새로운 스레드를 생성하고 전달할 Runnable나는이 때까지 이해하고, 그것을 작업을. 내가 그이다 혼동하는 부분은, 우리는 전화를 start()다른 이후에 새로운 스레드 방법을. 내 질문은 처음 호출 수행 할 때이다 start()에 메소드가 리턴 main()방법. 는 새로운 스레드 이후는 작업을 완료하거나 새로운 스레드가 작업을 실행하는 동안 반환합니까?

import java.util.Arrays;

public class Main   {
  public static final int DELAY = 10;
  public static final int STEPS = 100;
  public static final double MAX_AMOUNT = 1000;

  public static void main(String[] args)  {

    var bank = new Bank(4, 100000);
    Runnable task1 = () -> {
    try {

    for (int i = 0; i < STEPS; i++){

      double amount = MAX_AMOUNT * Math.random();
      bank.transfer(0, 1, amount);
      Thread.sleep((int) (DELAY * Math.random()));

    }

    }catch (InterruptedException e) {
    }

    };

    Runnable task2 = () ->{
    try{

    for (int i = 0; i < STEPS; i++){

      double amount = MAX_AMOUNT * Math.random();
      bank.transfer(2, 3, amount);
      Thread.sleep((int) (DELAY * Math.random()));

    }

    }
    catch (InterruptedException e){
    }

    };

    new Thread(task1).start();
    new Thread(task2).start();

  }

}

class Bank{

  private final double[] accounts;

  /**
   * Constructs the bank.
   * @param n the number of accounts
   * @param initialBalance the initial balance for each account
   *
   **/

  public Bank(int n, double initialBalance){

    accounts = new double[n];
    Arrays.fill(accounts, initialBalance);

   }

   /**
    * Transfers money from one account to another.
    * @param from the account to transfer from
    * @param to the account to transfer to 27
    * @param amount the amount to transfer 28
    **/

   public void transfer(int from, int to, double amount){

     if (accounts[from] < amount) return;
     System.out.print(Thread.currentThread());
     accounts[from] -= amount;
     System.out.printf(" %10.2f from %d to %d", amount, from, to);
     accounts[to] += amount;
     System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());

   }
   /**
    * Gets the sum of all account balances.
    * @return the total balance 42
    **/

   public double getTotalBalance(){

     double sum = 0;
     for (double a : accounts)
       sum += a;
     return sum;

   }

   /**
    * Gets the number of accounts in the bank.
    * * @return the number of accounts 56
    **/

   public int size(){

     return accounts.length;

   }

}
데이브 데인 :

시작 방법은 즉시 반환 및 주 스레드는 계속 실행됩니다.

당신은이 작은 Proggy이 일어나는 볼 수 있습니다 :

import java.time.Duration;
import java.time.ZonedDateTime;

public class Runner {

    public static void main(final String[] args) throws Exception {

        final Runnable runner = () -> {
            System.out.println(ZonedDateTime.now() + " " + Thread.currentThread().getName() + " Running...");

            sleep(Duration.ofSeconds(9));

            System.out.println(ZonedDateTime.now() + " " + Thread.currentThread().getName() + " Done.");
        };

        System    .out.println(ZonedDateTime.now() + " " + Thread.currentThread().getName() + " starting Thread 1...");
        new Thread(runner).start();

        System    .out.println(ZonedDateTime.now() + " " + Thread.currentThread().getName() + " starting Thread 2...");
        new Thread(runner).start();

        sleep(Duration.ofSeconds(3));

        System    .out.println(ZonedDateTime.now() + " " + Thread.currentThread().getName() + " done.");
    }

    private static void sleep(final Duration duration) {
        try {
            Thread.sleep(duration.toMillis());
        }
        catch (final InterruptedException e) {}
    }
}

추천

출처http://10.200.1.11:23101/article/api/json?id=478674&siteId=1