Java Technologies Thread Creation Example: Thread তৈরি এবং Runnable Interface ব্যবহার গাইড ও নোট

379

Java তে Thread Creation এবং Runnable Interface ব্যবহার করা একটি সাধারণ প্র্যাকটিস যা multithreading প্রোগ্রামিংয়ের মাধ্যমে একাধিক কার্যক্রম (tasks) একসাথে বা параллেলভাবে চালানোর সুবিধা দেয়। Thread হল একটি সম্পূর্ণ কর্মক্ষম একক যা CPU এর মাধ্যমে একযোগভাবে কাজ করতে পারে। Java তে Thread তৈরি করার দুটি প্রধান উপায় আছে:

  1. Thread Class এক্সটেন্ড করে।
  2. Runnable Interface ইমপ্লিমেন্ট করে।

এখানে আমরা Runnable Interface ব্যবহার করে Thread তৈরি করার উদাহরণ দেখাবো।

Runnable Interface ব্যবহার করে Thread তৈরি

Runnable Interface হল একটি ফাংশনাল ইন্টারফেস, যা একটি মাত্র run() মেথড নিয়ে কাজ করে। আপনি Runnable ইন্টারফেস ইমপ্লিমেন্ট করে একটি ক্লাস তৈরি করতে পারেন, যা থ্রেডের কার্যকারিতা (logic) কন্ট্রোল করবে।

ধাপ 1: Runnable Interface ইমপ্লিমেন্ট করা

// ThreadExample.java
public class ThreadExample implements Runnable {
    // run() method is the entry point for the thread
    @Override
    public void run() {
        // Define the task that the thread will execute
        System.out.println(Thread.currentThread().getId() + " is running the task.");
    }

    public static void main(String[] args) {
        // Create an object of the class that implements Runnable
        ThreadExample task = new ThreadExample();
        
        // Create two threads to run the same task
        Thread thread1 = new Thread(task);
        Thread thread2 = new Thread(task);
        
        // Start both threads
        thread1.start();
        thread2.start();
        
        // Print the main thread's information
        System.out.println(Thread.currentThread().getId() + " is the main thread.");
    }
}

কোড ব্যাখ্যা:

  1. Runnable Interface ইমপ্লিমেন্ট করা:
    • ThreadExample ক্লাসটি Runnable ইন্টারফেস ইমপ্লিমেন্ট করছে এবং এর মধ্যে run() মেথড ডিফাইন করা হয়েছে। run() মেথড হলো সেই মেথড যা থ্রেড চালানোর সময় কার্যকরী হবে।
  2. Thread Creation:
    • Thread thread1 = new Thread(task); — এখানে task হচ্ছে Runnable ইন্টারফেস ইমপ্লিমেন্ট করা একটি অবজেক্ট।
    • Thread ক্লাসের কনস্ট্রাক্টরে Runnable অবজেক্ট দেয়া হয়, যার মাধ্যমে থ্রেডটি run() মেথডটি চালাবে।
  3. Thread Start:
    • thread1.start(); এবং thread2.start();start() মেথডটি থ্রেডকে কার্যকর করে, অর্থাৎ এটি থ্রেডের কার্যক্রম শুরু করবে। একাধিক থ্রেড একই Runnable অবজেক্ট ব্যবহার করতে পারে, কিন্তু তারা নিজেদের নিজস্ব execution context এ কার্যকর হবে।
  4. Main Thread:
    • System.out.println(Thread.currentThread().getId() + " is the main thread."); — এটি মূল থ্রেড (main thread) এর আইডি এবং নাম প্রিন্ট করে, যেটি সবথেকে প্রথমে কার্যকরী হয়।

উদাহরণ আউটপুট:

1 is the main thread.
12 is running the task.
13 is running the task.

ব্যাখ্যা:

  • 1 is the main thread. মূল থ্রেডের ID প্রিন্ট করা হয়েছে।
  • 12 এবং 13 হল নতুন থ্রেডের ID যেগুলি কার্যকরী হয়ে তাদের respective কাজ চালাচ্ছে। এটি প্রতিটি থ্রেডের run() মেথডের কাজ করার সময় প্রদর্শিত হয়।

ধাপ 2: Thread Class ব্যবহার করা (Alternating Method)

এছাড়া আপনি Thread Class এক্সটেন্ড করে নিজের থ্রেড তৈরি করতে পারেন, তবে সাধারণত Runnable Interface ব্যবহার করা হয় কারণ এটি একাধিক ক্লাসকে একসাথে একটি থ্রেডের কার্যকারিতা ব্যবহার করার সুযোগ দেয়।

// ThreadClassExample.java
public class ThreadClassExample extends Thread {
    @Override
    public void run() {
        // Define the task that the thread will execute
        System.out.println(Thread.currentThread().getId() + " is executing its task.");
    }

    public static void main(String[] args) {
        // Create two threads using the extended Thread class
        ThreadClassExample thread1 = new ThreadClassExample();
        ThreadClassExample thread2 = new ThreadClassExample();

        // Start both threads
        thread1.start();
        thread2.start();
        
        // Print the main thread's information
        System.out.println(Thread.currentThread().getId() + " is the main thread.");
    }
}
  • Thread Creation Java তে Runnable Interface ব্যবহার করে করা হয়, যেখানে আপনি একটি run() মেথড ডিফাইন করেন এবং পরে Thread ক্লাসের মাধ্যমে তাকে কার্যকরী করেন।
  • Runnable Interface এবং Thread Class দুটি পদ্ধতিই কার্যকরী হলেও, সাধারণভাবে Runnable Interface ব্যবহৃত হয়, কারণ এটি একাধিক ক্লাসকে একসাথে থ্রেড কার্যকর করার সুযোগ দেয়।
  • Java তে multithreading আপনার প্রোগ্রামে একাধিক কাজ সমান্তরালে চলানোর ক্ষমতা প্রদান করে, যা অ্যাপ্লিকেশন পারফরম্যান্স বৃদ্ধি করতে সহায়ক।
Content added By
Promotion

Are you sure to start over?

Loading...