1、类方法中的线程
package com.wyq.thread;
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("run方法中的线程。。。");
}
}
2、主方法中的线程
package com.wyq.thread;
public class TestThre {
public static void main(String[] args) {
MyThread my = new MyThread();
//方法那边的线程启动
my.start();
//主方法中的线程启动
System.out.println("主线程。。。。。");
}
}
3、线程的运行原理
package com.wyq.thread;
public class MyThread2 extends Thread{
@Override
public void run() {
for(int i = 0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"———————————————————————>"+i);
}
}
}
package com.wyq.thread;
public class TestThread {
public static void main(String[] args) {
MyThread2 my = new MyThread2();
my.start();
for(int i = 0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"+++++++++》"+i);
}
}
}
在线程中,使用循环,可以使得抢占CPU的效率更加明显。
多线程实际上是在抢占内存中的CPU资源