Turn on multithreading
1, Inherit thread
2, overriding the Run method
3, write the code that will be executed in the Run method
4, create a subclass object for the thread class
5, turn on thread
/** * @param args */ public static void main(String[] args) { MyThread mt = new MyThread(); //4,创建Thread类的子类对象 mt.start(); //5,开启线程 for(int i = 0; i < 1000; i++) { System.out.println("main()"); } }}class MyThread extends Thread { //1,继承Thread public void run() { //2,重写run方法 for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中 System.out.println("aaa"); } }
public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); for (int i = 0; i <10000; i++) { System.out.println("mian()"); } }}class MyThread extends Thread{ public void run(){ for (int i = 0; i < 10000; i++) { System.out.println("run()"+i); } }
Java Multithreaded Learning-a