• Home
  • About Us
  • Courses
  • Blog
    • ServiceNow
    • Java
    • Web Technologies
    • Amazon Web Services (AWS)
    • DevOps
    • Google Cloud
  • Contact
    • RegisterLogin
      • GET STARTED
    Java4all - ServiceNow, AWS, Java and Cloud TechnologiesJava4all - ServiceNow, AWS, Java and Cloud Technologies
    • Home
    • About Us
    • Courses
    • Blog
      • ServiceNow
      • Java
      • Web Technologies
      • Amazon Web Services (AWS)
      • DevOps
      • Google Cloud
    • Contact
      • RegisterLogin
        • GET STARTED

      Java

      • Home
      • All courses
      • Java
      • Multithreading in Java Tutorial
      CoursesBackendJavaMultithreading in Java Tutorial
      • Table of Content 7

        • Lecture1.1
          What is Multi-Threading in Java? 05 min
        • Lecture1.2
          Thread Creation 10 min
        • Lecture1.3
          Thread Sleeping 10 min
        • Lecture1.4
          Thread Scheduler 05 min
        • Lecture1.5
          Thread Joining 20 min
        • Lecture1.6
          Life Cycle of a Thread 15 min
        • Quiz1.1
          Multithreading in Java Online Test 26 questions

        Thread Joining

        • This allows to wait one thread until the task of other thread is completed.
        • join() is a method used for thread joining.
        • In main thread -> t1.join()

        Main thread will be joined at the end of t1.

        • In t1 thread -> t2.join

        T1 thread will be joined at the end of t2.

        import java.io.*;
        class ThreadJoin extends Thread{
        public void run(){
        for (int i = 0; i< 2; i++){
        try{
        Thread.sleep(500);
        System.out.println(“Thread: “+ Thread.currentThread().getName());
        }
        catch(Exception ex)
        {
        System.out.println(“Exception caught”);
        }
        System.out.println(i);
        }
        }
        }
        class Intellipaat{
        public static void main (String[] args)
        {
        ThreadJoining t1 = new ThreadJoining();
        ThreadJoining t2 = new ThreadJoining();t1.start();
        try{
        System.out.println(“Thread: ”+ Thread.currentThread().getName());
        t1.join();
        }
        catch(Exception ex)
        {
        System.out.println(“Exceptioncaught”);
        }t2.start();
        try
        {
        System.out.println(“Thread: ”+ Thread.currentThread().getName());
        t2.join();
        }catch(Exception e){
        System.out.println(“Exception caught”);}
        }
        }

        Output:
        Thread: main
        Thread: Thread0
        Thread: Thread0
        1
        Thread: main
        Thread: Thread1
        Thread: Thread1
        1

        Prev Thread Scheduler
        Next Life Cycle of a Thread

        Leave A Reply Cancel reply

        You must be logged in to post a comment.

        All Courses

        • AngularJS
        • AWS
        • Backend
          • J2EE
          • Java
        • J2EE
        • Java
        • ServiceNow

        Latest Courses

        ServiceNow Service Catalog Practice Test

        ServiceNow Service Catalog Practice Test

        Free
        ServiceNow Certified System Administrator Practice Test – Fundamental – 2

        ServiceNow Certified System Administrator Practice Test – Fundamental – 2

        Free
        ServiceNow Certified System Administrator Practice Test – Knowledge Management

        ServiceNow Certified System Administrator Practice Test – Knowledge Management

        Free

        Become an Instructor

        Join our community of students around the world and sell your courses.

        Learn More

        BBS Learning Powered by BBS Technologies.

        • Privacy
        • Terms
        • Sitemap
        • Purchase

        Login with your site account

        Lost your password?

        Not a member yet? Register now

        Register a new account

        Are you a member? Login now