কম্পিউটার টিউটোরিয়াল

জাভা কনকারেন্সি - join() মেথড


join ফাংশন

এই ফাংশনটি একটি থ্রেডের এক্সিকিউশন শুরু থেকে অন্য একটি থ্রেডের এক্সিকিউশন শেষ পর্যন্ত জড়াতে ব্যবহৃত হয়। এইভাবে, নিশ্চিত করা হয় যে প্রথম থ্রেডটি দ্বিতীয় থ্রেড এক্সিকিউশন বন্ধ না হওয়া পর্যন্ত চলবে না। এই ফাংশন একটি নির্দিষ্ট মিলিসেকেন্ড থ্রেড টার্মিনেট হওয়ার জন্য অপেক্ষা করে।

একটি উদাহরণ নিচে দেখানো হলো:

উদাহরণ

import java.lang.*; public class Demo implements Runnable{    public void run(){       Thread my_t = Thread.currentThread();       System.out.println("The name of the current thread is " + my_t.getName());       System.out.println("Is the current thread alive? " + my_t.isAlive());    }    public static void main(String args[]) throws Exception{       Thread my_t = new Thread(new Demo());       System.out.println("The instance has been created and started");       my_t.start();       my_t.join(30);       System.out.println("The threads will be joined after 30 milli seconds");       System.out.println("The name of the current thread is " + my_t.getName());       System.out.println("Is the current thread alive? " + my_t.isAlive());    } }

আউটপুট

The instance has been created and started The threads will be joined after 30 milli seconds The name of the current thread is Thread-0 The name of the current thread is Thread-0 Is the current thread alive? true Is the current thread alive? true

Demo ক্লাস Runnable ক্লাস ইমপ্লিমেন্ট করে। একটি 'run' ফাংশন সংজ্ঞায়িত করা হয়েছে যা করেন্ট থ্রেডকে একটি নতুন তৈরি করা Thread হিসেবে অ্যাসাইন করে। main ফাংশনে, থ্রেডের একটি নতুন ইনস্ট্যান্স তৈরি করা হয়েছে, এবং 'start' ফাংশন ব্যবহার করে এটি শুরু করা হয়েছে। এই থ্রেডটি নির্দিষ্ট সময়ের পর অন্য একটি থ্রেডের সাথে জড়ানো হয়েছে। প্রাসঙ্গিক মেসেজ স্ক্রিনে প্রদর্শন করা হয়েছে।

  1. জাভাতে Thread.start() বদলে Thread.run() কখন কল করা উচিত?

  2. জাভাতে অ্যানোনিমাস ক্লাস ব্যবহার করে থ্রেড তৈরি করা

  3. জাভাতে isDaemon() মেথডের গুরুত্ব

  4. জাভা ৮-এ ইন্টারফেস এনহ্যান্সমেন্ট