:是进程中的单个顺序控制流,是一条执行路径。
多线程的执行方式:
- public class MyThreadDome {
- public static void main(String[] args){
- MyThread my1=new MyThread();
- MyThread my2=new MyThread();
-
- //调用run()方法并没有启动线程
- // my1.run();
- // my2.run();
-
- //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
- my1.start();
- my2.start();
- }
- }
-
- public class MyThread extends Thread{
- @Override
- public void run() {
- super.run();
- for(int i=0;i<100;i++){
- System.out.println(i);
- }
- }
- }
多线程结果:

重写run()方法?
run()是用来封装被线程执行的代码。
run()与start()方法区别?
run()是用来封装被线程执行的代码。直接调用,相当于普通方法。
start():启动线程,然后由JVM调用此线程的run()方法。
Thread类中设置和获取线程名称的方法
- public class MyThreadDome {
- public static void main(String[] args){
- MyThread my1=new MyThread();
- MyThread my2=new MyThread();
-
- //调用run()方法并没有启动线程
- // my1.run();
- // my2.run();
-
- my1.setName("张三");
- my2.setName("李四");
-
- // MyThread my1=new MyThread("王五");
- // MyThread my2=new MyThread("赵茜");
- //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
- my1.start();
- my2.start();
- }
- }
- public class MyThread extends Thread{
- // public MyThread(){}
- // public MyThread(String name){
- // super(name);
- // }
- @Override
- public void run() {
- super.run();
- for(int i=0;i<100;i++){
- System.out.println(getName()+":"+i);
- }
- }
- }
- /*
- private static synchronized int nextThreadNum() {
- return threadInitNumber++;
- }
- */


- public class MyThreadDome {
- public static void main(String[] args){
- // MyThread my1=new MyThread();
- // MyThread my2=new MyThread();
- //
- // //调用run()方法并没有启动线程
- // // my1.run();
- // // my2.run();
- //
- // my1.setName("张三");
- // my2.setName("李四");
-
- MyThread my1=new MyThread("王五");
- MyThread my2=new MyThread("赵茜");
- //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
- my1.start();
- my2.start();
- }
- }
- public class MyThread extends Thread{
- public MyThread(){}
- public MyThread(String name){
- super(name);
- }
- @Override
- public void run() {
- super.run();
- for(int i=0;i<100;i++){
- System.out.println(getName()+":"+i);
- }
- }
- }
- /*
- private static synchronized int nextThreadNum() {
- return threadInitNumber++;
- }
- */


//static Thread currentThread()返回对当前正在执行的线程对象的引用:
- public class MyThreadDome {
- public static void main(String[] args){
- // MyThread my1=new MyThread();
- // MyThread my2=new MyThread();
- //
- // //调用run()方法并没有启动线程
- // // my1.run();
- // // my2.run();
- //
- // my1.setName("张三");
- // my2.setName("李四");
-
- // MyThread my1=new MyThread("王五");
- // MyThread my2=new MyThread("赵茜");
- // //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
- // my1.start();
- // my2.start();
- System.out.println(Thread.currentThread().getName());
- }
- }
- public class MyThread extends Thread{
- public MyThread(){}
- public MyThread(String name){
- super(name);
- }
- @Override
- public void run() {
- super.run();
- for(int i=0;i<100;i++){
- System.out.println(getName()+":"+i);
- }
- }
- }
- /*
- private static synchronized int nextThreadNum() {
- return threadInitNumber++;
- }
- */

分时调度模型
抢占式调度模型(Java使用)
Thread类中设置和获取线程优先级的方法:
》》public final int getPriority():返回此线程的优先级
》》public final void setPriority(int newPriority):更改此线程的优先级
- public class MyThreadDome {
- public static void main(String[] args){
- ThreadPriority tpy1=new ThreadPriority();
- ThreadPriority tpy2=new ThreadPriority();
- ThreadPriority tpy3=new ThreadPriority();
-
- tpy1.setName("张三");
- tpy2.setName("李四");
- tpy3.setName("王五");
-
- tpy1.start();
- tpy2.start();
- tpy3.start();
- }
- }
- public class ThreadPriority extends Thread{
- public void run(){
- for(int i=0;i<100;i++)
- {
- System.out.println(getName()+":"+i);
- }
- }
- }


- public class MyThreadDome {
- public static void main(String[] args){
- ThreadPriority tpy1=new ThreadPriority();
- ThreadPriority tpy2=new ThreadPriority();
- ThreadPriority tpy3=new ThreadPriority();
-
- tpy1.setName("张三");
- tpy2.setName("李四");
- tpy3.setName("王五");
-
- // //public final int getPriority():返回此线程的优先级 获取优先级——都是5
- // System.out.println(tpy1.getPriority());//5
- // System.out.println(tpy2.getPriority());//5
- // System.out.println(tpy3.getPriority());//5
-
- //public final void setPriority(int newPriority):更改此线程的优先级
-
- //获取优先级的范围:
- System.out.println("优先级的范围:");
- System.out.println(Thread.MAX_PRIORITY);
- System.out.println(Thread.MIN_PRIORITY);
- System.out.println(Thread.NORM_PRIORITY);
-
- System.out.println("正确设置优先级:");
- tpy1.setPriority(5);
- tpy2.setPriority(10);
- tpy3.setPriority(1);
-
- tpy1.start();
- tpy2.start();
- tpy3.start();
- }
- }
- public class ThreadPriority extends Thread{
- public void run(){
- for(int i=0;i<100;i++)
- {
- System.out.println(getName()+":"+i);
- }
- }
- }