小编Emr*_*san的帖子

如何使用Java在同一个类中同时运行2个方法

我想在Java中使用同一个对象同时在同一个类中使用2个方法.例如:

public class aThread extends Thread {

    int countA = 0;
    int countB = 0;

    int countA(){
        for (int i = 0; i < 1000; i++) {
            countA++;
        }
        return countA;
    }
    int countB(){
        for (int i = 0; i < 1000; i++) {
            countB++;
        }
        return countB;
    }
    @Override
    public void run() {
        super.run(); 
        //should there be something here?
    }
}
Run Code Online (Sandbox Code Playgroud)

并在另一种方法中使用此方法:

public class MainClass {

    public static void main(String[] args) {

        aThread myThread = new aThread();

        myThread.countA(); //I …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading

2
推荐指数
1
解决办法
8484
查看次数

标签 统计

concurrency ×1

java ×1

multithreading ×1