线程常用操作方法

2020-11-25  本文已影响0人  曾梦想仗剑天涯

线程的命名和取得

//观察线程的命名操作
package com.company;
class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        new Thread(mt, "线程A").start();      //设置了线程名字
        new Thread(mt).start();     //未设置线程名字
        new Thread(mt, "线程B").start();      //设置了线程名字
    }
}
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
    return threadInitNumber++;
}
package com.company;
class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}    
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        MyThread mt = new MyThread();
        new Thread(mt, "线程A").start();      //设置了线程名字
        mt.run();       //对象直接调用run()方法
    }
}
//子线程处理
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        System.out.println("执行任务一");
        new Thread(() -> {
            int temp = 0;
            for (int x = 0; x < Integer.MAX_VALUE; x++) {
                temp += x;
            }
        });
        System.out.println("执行任务二");
        System.out.println("执行任务三");
    }
}

线程休眠

//观察休眠处理
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        new Thread(() -> {
            for (int x = 0; x < 10; x++) {
                System.out.println(Thread.currentThread().getName() + "、x = " + x);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程对象").start();
    }
}
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        for (int num = 0; num < 5; num ++) {
            new Thread(() -> {
                for (int x = 0; x < 10; x++) {
                    System.out.println(Thread.currentThread().getName() + "、x = " + x);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "线程对象-" + num).start();
        }
    }
}
此图来源于李兴华老师

线程中断

//观察线程中断处理操作
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                System.out.println("中断线程");
            }
        });
        thread.start();
        Thread.sleep(1000);
        if (!thread.isInterrupted()) {
            thread.interrupt();
        }
    }
}

线程的强制运行

//观察一个没有强制执行的程序
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(() -> {
            for (int x = 0; x < 100; x++) {
                System.out.println(Thread.currentThread().getName() + "、x=" + x);
            }
        });
        thread.start();
        for (int x = 0; x < 100; x ++) {
            System.out.println("number=" + x);
        }
    }
}
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        Thread threadMain = Thread.currentThread();    //获得主线程
        Thread thread = new Thread(() -> {
            for (int x = 0; x < 100; x++) {
                if (x == 3) {
                    try {
                        threadMain.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread().getName() + "、x=" + x);
            }
        });
        thread.start();
        for (int x = 0; x < 100; x ++) {
            System.out.println("number=" + x);
        }
    }
}

线程的礼让

//使用礼让操作
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        Thread threadMain = Thread.currentThread();
        Thread thread = new Thread(() -> {
            for (int x = 0; x < 100; x++) {
                if(x % 3 == 0) {
                    Thread.yield();
                    System.out.println("进行了礼让");
                }
                System.out.println(Thread.currentThread().getName() + "、x=" + x);
            }
        });
        thread.start();
        for (int x = 0; x < 100; x ++) {
            System.out.println("number=" + x);
        }
    }
 }

线程优先级

//观察优先级
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        Runnable run = () -> {
            for (int x = 0; x < 10; x++) {
                System.out.println(Thread.currentThread().getName() + "线程执行");
            }
        };
        Thread threadA = new Thread(run, "线程对象A");
        Thread threadB = new Thread(run, "线程对象B");
        Thread threadC = new Thread(run, "线程对象C");
        threadA.setPriority(Thread.MIN_PRIORITY);
        threadB.setPriority(Thread.MIN_PRIORITY);
        threadC.setPriority(Thread.MAX_PRIORITY);
        threadA.start();
        threadB.start();
        threadC.start();
    }
}
package com.company;
public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        System.out.println(Thread.currentThread().getPriority());    //5
        System.out.println(new Thread().getPriority());    //5
    }
}
上一篇下一篇

猜你喜欢

热点阅读