注册时间有点晚,补一下onenote的记录
thread.java中的start()方法,表明线程已经准备就绪,等待调用线程对象的run方法,是异步的,谁抢到时间片,谁运行
thread.java中的run方法,是main方法主线程对象调用,每个主线程对象运行完之后才会有后续对象再调用,是同步的
public Test(String name) { super(name); } @Override public void run() { int i = 10; while (i-- > 0) { System.out.println(getName()+"--------" + i); } } public static void main(String[] args) { Test test1 =new Test("A"); Test test2 =new Test("B"); test1.run(); test2.run(); } }
A--------9 A--------8 A--------7 A--------6 A--------5 A--------4 A--------3 A--------2 A--------1 A--------0 B--------9 B--------8 B--------7 B--------6 B--------5 B--------4 B--------3 B--------2 B--------1 B--------0
用run的话,A对象运行完,B对象才会运行
public Test(String name) { super(name); } @Override public void run() { int i = 10; while (i-- > 0) { System.out.println(getName() + "--------" + i); } } public static void main(String[] args) { Test test1 = new Test("A"); Test test2 = new Test("B"); test1.start(); test2.start(); }
B--------9 B--------8 A--------9 B--------7 A--------8 B--------6 A--------7 B--------5 A--------6 B--------4 A--------5 B--------3 A--------4 B--------2 A--------3 B--------1 A--------2 B--------0 A--------1 A--------0
start的话,AB对象异步运行
以上个人理解,欢迎各位大佬评论修正