Java 测试多线程代码

(0 comments)

为了演示系统中多线程的运作,写了一个简单的测试CPU多线程处理代码。

TestCPU.java,代码如下:

public class TestCPU implements Runnable {
   public static void main(String[] args) {
       Thread a = new Thread(new TestCPU(), "a");
       Thread b = new Thread(new TestCPU(), "b");
       a.start();
       b.start();
   }

   public void run() {
       long i = 0;
       while (true) {
           i++;
           System.out.println(Thread.currentThread().getId() + ": " + i);
       }
   }
}

Current rating: 5

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required