java多线程体验1
多线程估计大家都在用,大家每天面对的操作系统都是多线程的应用,呵呵,以前的dos系统就是经典的单线程体现。编程也用了不少多线程,是时候写个文章总结一下,以便对知识的总结和交流。
单线程的程序结构:
public class TestThread { public static void main(String[] args) { MethodDemo demo=new MethodDemo(); demo.run(); for (int i = 0; i < 5; i++) { System.out.println("Main" + i); } }}class MethodDemo implements Runnable { public void run() { for (int i = 0; i < 5; i++) { System.out.println("MethodDemo" + i); } }}
public class TestThread { public static void main(String[] args) { MethodDemo demo = new MethodDemo(); Thread s = new Thread(demo); s.start(); for (int i = 0; i < 5; i++) { System.out.println("Main" + i); } }}class MethodDemo implements Runnable { public void run() { for (int i = 0; i < 5; i++) { System.out.println("MethodDemo" + i); } }}