zoukankan      html  css  js  c++  java
  • 线程基础

    一、进程和线程区别:

    进程就是系统中最小执行单元。

    线程是进程中最小执行单元

    进程中执行的功能通过线程来实现

    多个线程共享一个进程中所有资源(PC寄存器,上下文,本地栈)

    单进程单线程-> 一人在一个桌子上面吃饭

    单进程多线程-> 多个人在一个桌子上面吃饭

    多进程多线程-> 每一个人在各自的桌子上面吃饭

    -- 由系统调度器调度

    -- 线程运行方式:

    程序->创建一个线程->start->runnable

    系统调度就行状态的中线程->running

    -- java中怎么创建线程

    Thread

    Runnable

     

     无线程

    package com.nothread.demo;
    
    public class NoThreadDemo {
        
        public static void main(String[] args) {
            // 默认主线程中的执行顺序是从上而下执行的
            MyClass cl = new MyClass();
            cl.run1();
            cl.run2();
        }
    
        private static class MyClass {
            public void run1() {
                while (true) {
                    System.out.println("第一条通道可以到目的地");
                }
            }
    
            public void run2() {
                while (true) {
                    System.out.println("第二条通道可以到目的地");
                }
            }
        }
    }

    继承Thread

    package com.thread.demo;
    
    /**
     * 使用线程
     * 
     * @author Administrator
     *
     */
    public class TestThreadDemo {
        public static void main(String[] args) {
            System.out.println("main thread started");
            // 创建2个线程
            GameThread game = new GameThread();
            VoiceThread voice = new VoiceThread();
            // 启动线程 start 
            game.start();
            voice.start();
            // 停止(太暴力终止过程中会出现数据错误问提)
            try {
                game.join();
                voice.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            System.out.println("main thread ended~~");
        }
        
        /**
         * 游戏线程类
         * @author Administrator
         *
         */
        private static class GameThread extends Thread {
            @Override
            public void run() {
                for (int i = 0; i <10; i++) {
                    System.out.println("执行打游戏");
                }
            }
        }
        
        /**
         * 开黑线程类
         * @author Administrator
         *
         */
        private static class VoiceThread extends Thread {
            @Override
            public void run() {
                for (int i = 0; i <10; i++) {
                    System.out.println("执行语言通话");
                }
            }
        }
    }

     实现Runnable方式

    package com.thread.demo;
    
    public class TestThreadDemo1 {
        public static void main(String[] args) {
            System.out.println(Thread.currentThread().getName());
            Thread t1 = new Thread(new MyRunnable(), "AAAA");
            Thread t2 = new Thread(new MyRunnable(), "BBBB");
            t1.start();
            t2.start();
        }
        
        private static class MyRunnable implements Runnable {
            @Override
            public void run() {
                for (int i = 0; i < 10; i ++) {
                    System.out.println(Thread.currentThread().getName()+"执行Runnable接口");
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"执行Runnable接口");
                }
            }
            
        }
    }

    线程状态

    package com.thread.demo;
    
    public class ThreadStateDemo {
    
        public static void main(String[] args) {
            StateThread st = new StateThread("AAAA");
            st.start();
            try {
                st.join(20000000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("main end~");
        }
        
        private static class StateThread extends Thread {
            
            public StateThread(String name) {
                super(name);
            }
            
            @Override
            public void run() {
                for (int i = 0 ; i < 10; i++) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("执行线程内容");
                }
            }
        }
    }

     jps

    jstck

    线程状态参考https://www.cnblogs.com/aspirant/p/8900276.html

  • 相关阅读:
    python 基础2.5 循环中continue与breake用法
    python 基础 2.4 while 循环
    python 基础 2.3 for 循环
    python 基础 2.2 if流程控制(二)
    python 基础 2.1 if 流程控制(一)
    python 基础 1.6 python 帮助信息及数据类型间相互转换
    python 基础 1.5 python数据类型(四)--字典常用方法示例
    Tornado Web 框架
    LinkCode 第k个排列
    LeetCode 46. Permutations
  • 原文地址:https://www.cnblogs.com/sunBinary/p/10526668.html
Copyright © 2011-2022 走看看