• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Thread Class in Java

April 5, 2022 Java

In this post, you will learn how to work with the Thread class so that you can use that same knowledge and thus evolve.

  • this class allows the creation/control of a certain process and it is possible to define its priority and obtain its status;
  • the thread is basically a task.
Thread (object).start() Initiation determined thread that had been instantiated
Thread (object).join() Blocks the initial thread (the calling thread) until the current thread ends
Thread.sleep() Suspends the current thread for the specified time (time in milliseconds)
package com.caffeinealgorithm.programminginjava;

public class ThreadClass {
  private Thread thread;

  public void Run() {
    thread = new Thread(this::executeThread);
    thread.start();

    for (int index = 1; index <= 5; index++) {
      System.out.printf("Run(): #%d\n", index);

      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException exception) {
        System.out.println(exception);
      }
    }

    System.out.println("The Run() thread terminated.");

    try {
      thread.join();
    }
    catch (InterruptedException exception) {
      System.out.println(exception);
    }

    System.out.println("The executeThread() thread terminated.");
  }

  private void executeThread() {
    for (int index = 1; index <= 10; index++) {
      System.out.printf("executeThread(): #%d\n", index);

      try {
        Thread.sleep(1000);
      }
      catch (InterruptedException exception) {
        System.out.println(exception);
      }
    }
  }
}

/*
  Run(): #1
  executeThread(): #1
  Run(): #2
  executeThread(): #2
  Run(): #3
  executeThread(): #3
  Run(): #4
  executeThread(): #4
  Run(): #5
  executeThread(): #5
  The Run() terminated.
  executeThread(): #6
  executeThread(): #7
  executeThread(): #8
  executeThread(): #9
  executeThread(): #10
  The executeThread() thread terminated.
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved