Creating Threads in Java

Today in Java, I'm going to teach you how to create a program that will create threads. We all know that a thread is a basic processing unit in our computer to which an operating system allocates processor time, and more than one thread can be executing code inside a process. The Java Virtual Machine (JVM) allows an application to have multiple threads of execution running concurrently. Thus, threads have priority. It is just like a racing program. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of thread.java. 2. Import java.io package. Hence we will use an input/output in accessing files, with IOException.
  1. import java.io.IOException;
3. Provide a Thread extension in your classname and declare the following variables below.
  1. class thread extends Thread {
  2.     private String carName;
  3.     private long aWhile;
4. Create a constructor named thread that is the same with the class name with the parameters of String carName, long delay. Make the delay variable equal to the aWhile variable that we have created above. Make the setDaemon equals to True. A daemon thread is also a thread, that does not prevent the Java Virtual Machine(JVM) from exiting when the program finishes but the thread is still running.
  1.   public thread(String carName, long delay) {
  2.     this.carName = carName;
  3.     aWhile = delay;
  4.     setDaemon(true);
  5.   }
5. Create a method named run for running the thread when using the start keyword. This will display the carnames and have a delay in displaying it.
  1.  public void run() {
  2.     try {
  3.       while (true) {
  4.         System.out.print(carName);
  5.         sleep(aWhile);
  6.    
  7.       }
  8.     } catch (InterruptedException e) {
  9.       System.out.println(carName  + e);
  10.     }
  11.   }
6. In your Main, create three threads that have the carnames and the delay parameters.
  1.  public static void main(String[] args) {
  2.     Thread first = new thread("Toyota \n", 100L);
  3.     Thread second = new thread("Honda \n", 200L);
  4.     Thread third = new thread("Suzuki \n", 500L);
  5.     System.out.println("Press Enter when you have had enough...\n");
To start running threads, put this code below.
  1.     first.start();
  2.     second.start();
  3.     third.start();
To stop our thread from running, put this Try and Catch statement below. The thread will stop if we press Enter.
  1.     try {
  2.       System.in.read();
  3.       System.out.println("Enter pressed...\n");
  4.     } catch (IOException e) {
  5.       System.out.println(e);
  6.     }
Full source code:
  1. import java.io.IOException;
  2.  
  3. class thread extends Thread {
  4.        
  5.         private String carName;
  6.     private long aWhile;
  7.  
  8.  
  9.   public thread(String carName, long delay) {
  10.     this.carName = carName;
  11.     aWhile = delay;
  12.     setDaemon(true);
  13.   }
  14.  
  15.  
  16.   public void run() {
  17.     try {
  18.       while (true) {
  19.         System.out.print(carName);
  20.         sleep(aWhile);
  21.    
  22.       }
  23.     } catch (InterruptedException e) {
  24.       System.out.println(carName  + e);
  25.     }
  26.   }
  27.  
  28.  
  29.  
  30.   public static void main(String[] args) {
  31.     Thread first = new thread("Toyota \n", 100L);
  32.     Thread second = new thread("Honda \n", 200L);
  33.     Thread third = new thread("Suzuki \n", 500L);
  34.     System.out.println("Press Enter when you have had enough...\n");
  35.     first.start();
  36.     second.start();
  37.     third.start();
  38.     try {
  39.       System.in.read();
  40.       System.out.println("Enter pressed...\n");
  41.     } catch (IOException e) {
  42.       System.out.println(e);
  43.     }
  44.  
  45. }
  46.  
  47. }
Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment