关于java:线程相关的一些基本概念

6次阅读

共计 2119 个字符,预计需要花费 6 分钟才能阅读完成。

一、程序

例如 QQ、微信等这样的利用,即是程序。

二、过程

过程就是一个 程序 运行起来的状态。

三、线程

线程是一个 过程 中不同的执行门路。

业余一点的解释:
过程是 OS 分配资源的根本单位,线程是执行调度的根本单位。分配资源最重要的是:开拓独立的内存空间,线程再调度执行(即线程共享过程的内存空间,没有本人独立的内存空间)

四、纤程 / 协程

纤程是用户态的线程,线程中的线程;纤程的切换和调度不须要通过内核,而线程的切换是在内核态实现的。

1、与线程相比,纤程特点

1)占有资源少,个别 OS 的线程占用 1M,纤程 4K

2)切换比较简单

五、创立线程的办法

1、继承 Thread 类

2、实现 Runnable 接口

3、通过 Executor 线程池创立

/**
 * @author Java 和算法学习:周一
 */
public class CreateThread {

    private static class MyThread extends Thread {
        @Override
        public void run() {System.out.println("MyThread");
        }
    }

    private static class MyRun implements Runnable {
        @Override
        public void run() {System.out.println("MyRun");
        }
    }

    public static void main(String[] args) {new MyThread().start();
        new Thread(new MyRun()).start();

        new Thread(()->{System.out.println("lambda...");
        }).start();}

}

六、线程罕用的办法

1、Sleep 办法

让出以后执行的线程,给别的线程去运行

2、Yield 办法

谦让的退出一下,即让出 CPU 一下;此线程会到线程的期待队列里,CPU 有可能会再次执行该线程,但更大的可能是执行其余的线程。

3、Join 办法

进行执行以后的线程,转而去执行 join 的线程

/**
 * @author Java 和算法学习:周一
 */
public class SleepYieldJoin {public static void main(String[] args) {//        testSleep();
//        testYield();
        testJoin();}

    static void testSleep() {new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println("SleepA" + i);
                try {Thread.sleep(1);
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {for (int i = 0; i < 100; i++) {System.out.println("SleepB" + i);
            }
        }).start();}

    static void testYield() {new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println("yieldA" + i);
                if (i % 2 == 0) {Thread.yield();
                }
            }
        }).start();

        new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println("yieldB" + i);
                if (i % 2 == 0) {Thread.yield();
                }
            }
        }).start();}

    static void testJoin() {Thread t1 = new Thread(() -> {for (int i = 0; i < 10; i++) {System.out.println("A" + i);
                try {Thread.sleep(300);
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            try {t1.join();
            } catch (InterruptedException e) {e.printStackTrace();
            }

            for (int i = 0; i < 10; i++) {System.out.println("B" + i);
                try {Thread.sleep(300);
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            try {t2.join();
            } catch (InterruptedException e) {e.printStackTrace();
            }

            for (int i = 0; i < 10; i++) {System.out.println("c" + i);
                try {Thread.sleep(300);
                } catch (InterruptedException e) {e.printStackTrace();
                }
            }
        });

        // 不论 t1、t2、t3 启动程序怎么,输入后果都一样,这就能够保障 t1、t2、t3 按程序执行
        t3.start();
        t2.start();
        t1.start();}

}
正文完
 0