ตำแหน่งของคุณ:เว็บแทงบอล ถูกกฎหมาย - Thb999 > ดาวน์โหลดบาคาร่า > ข้อมูลอุตสาหกรรม >

Java Thread start() method

ผู้เขียน: ความนิยม:เวลา:2022-09-20 23:52

method of thread class is used to begin the execution of thread. The result of this method is two threads that are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

The start() method internally calls the run() method of Runnable interface to execute the code specified in the run() method in a separate thread.

The start thread performs the following tasks:

IllegalThreadStateException- This exception throws if the start() method is called more than one times.

Example 1: By Extending thread class

public class StartExp1 extends Thread public void run() System.out.println(Thread is running...); public static void main(String args[]) StartExp1 t1=new StartExp1(); // this will call run() method t1.start();

Example 2: By Implementing Runnable Interface

public class StartExp2 implements Runnable public void run() System.out.println(Thread is running...); public static void main(String args[]) StartExp2 m1=new StartExp2 (); Thread t1 =new Thread(m1); // this will call run() method t1.start();

Example 3: When you call the start() method more than one time

public class StartExp3 extends Thread public void run() System.out.println(First thread running...); public static void main(String args[]) StartExp3 t1=new StartExp3(); t1.start(); // It will through an exception because you are calling start() method more than one time t1.start();

First thread running... Exception in thread main ng.IllegalThreadStateException at java.lang.Thread.start(Thread.java:708) at StartExp3.main(StartExp3.java:12)

JavaTpoint offers too many high quality services. Mail us on[emailprotected], to get more information about given services.

JavaTpoint offers college campus training on Core Java, Advance Java, , Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at[emailprotected]Java Thread start() method

下一篇:WavingCat 上一篇:Time and Distance
คีย์เวิร์ดในบทความนี้:T1 starts