编程语言
首页 > 编程语言> > Java 线程实现两个方法同时运行

Java 线程实现两个方法同时运行

作者:互联网

public static void main(String[] args) throws Exception {
     
    Thread threadOne = new Thread(new Runnable() {
        public void run() {
            methodOne();
        }
    });
     
    Thread threadTwo = new Thread(new Runnable() {
        public void run() {
            methodTwo();
        }
    });
     
    // 执行线程
    threadOne.start();
    threadTwo.start();
     
    Thread.sleep(1000);
}
 
public static void methodOne() {
    System.out.println("Method one is running!");
}
 
public static void methodTwo() {
    System.out.println("Method two is running!");
}

标签:Java,threadOne,Thread,void,static,线程,new,public,运行
来源: https://blog.csdn.net/qq_39962271/article/details/116357480