You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java线程技术求助:如何从继承Thread的类中获取变量值

How to Access MyThread's runBoolean Variable in MyMaster

Hey there! Let's break down how to adjust your code so you can properly access the runBoolean variable from your MyThread instances in MyMaster. Here are the key fixes and explanations:

1. Use MyThread[] Instead of Thread[]

Right now, your threads array is typed as Thread, which means each element is treated as a generic thread object—you can't directly access the runBoolean field that's specific to your MyThread subclass. Changing the array type to MyThread[] lets you work directly with your custom thread instances.

2. Add a Getter for runBoolean (Encapsulation Best Practice)

Instead of accessing the runBoolean field directly, it's better to add a public getter method in MyThread. This follows encapsulation principles, keeps your code maintainable, and avoids exposing internal state unnecessarily.

3. Fix join() Placement (For Concurrent Execution)

Calling join() inside your loop means you wait for each thread to finish before starting the next one. If you want both threads to run in parallel, move the join() calls to a separate loop after starting all threads.

4. Implement the run() Method in MyThread

Your MyThread class extends Thread but doesn't override the run() method—this is critical because it defines what the thread actually does when it starts. I'll add a sample implementation that sets runBoolean (adjust this to match your actual business logic).

Corrected MyThread Class

public class MyThread extends Thread {
    int myBil;
    boolean runBoolean;

    public MyThread(int bil) {
        myBil = bil;
        start(); // Starting the thread here is acceptable, though you could also start it outside the constructor for more control
    }

    // Getter method to safely access runBoolean
    public boolean isRunBoolean() {
        return runBoolean;
    }

    // Override run() to define the thread's behavior
    @Override
    public void run() {
        // Replace this with your actual thread logic
        try {
            // Simulate work being done by the thread
            Thread.sleep(1500);
            runBoolean = true; // Set the variable based on your logic
            System.out.println("Thread " + myBil + " completed work, runBoolean = " + runBoolean);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            runBoolean = false;
        }
    }
}

Corrected MyMaster Class

public class MyMaster {
    public static void main(String[] args) throws InterruptedException {
        MyThread[] threads = new MyThread[2]; // Use MyThread[] instead of Thread[]

        // Start all threads first
        for (int i = 1; i <= 2; i++) {
            threads[i-1] = new MyThread(i);
        }

        // Wait for each thread to finish, then access runBoolean
        for (MyThread thread : threads) {
            thread.join();
            // Use the getter to retrieve runBoolean
            System.out.println("From Master: Thread " + thread.myBil + " runBoolean = " + thread.isRunBoolean());
        }
    }
}

Important Note for Thread Safety

If you need to access runBoolean while the thread is still running (not just after it finishes), replace the plain boolean with AtomicBoolean from java.util.concurrent.atomic. This prevents race conditions that can occur when multiple threads read/write the variable simultaneously.

内容的提问来源于stack exchange,提问作者frianH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:58:33