zoukankan      html  css  js  c++  java
  • javaoop_多线程

    在java 虚拟机进程中,执行程序代码的任务是由线程来完成的,每个线程都有一个独立的程序计算器和方法调用栈(method invocation stack)

    每当用java命令启动一个Java虚拟机进程是,Java虚拟机都会创建一个主程序,该程序从程序入口main()方法开始执行。

    进程:应用程序的执行实例,有独立的内存空间和系统资源,

    线程:cpu调度和分派的基本单位,进程中执行运算的最小单位,可完成一个独立的顺序控制流程

    多线程:如果在一个进程中同时运行了多个线程,用来完成不同的工作,多个线程交替占用cpu资源,而非真正的执行

    多线程的好处:
    1.充分利用cpu的资源
    2.简化编程模型
    3.带来良好的用户体验

    用户还可以创建自己的线程,它将和主线程并发运行,创建有两种方式:

     继承Thread类
    1.编写简单,可直接操作线程
    2.适用于单继承
    实现Runnable接口
    1.避免单继承局限性
    2.便于共享资源

    Thread类

    java提供了Java.lang.Thread类支持多线程编程
    主线程
    main()方法即为主线程入口
    产生其他子线程的线程
    必须最后完成执行,
    因为它执行各种关闭动作

    run()方法:包含线程运行时执行的代码

    start()方法:用于启动线程

    用户的线程类只需要继承Thread类 覆盖Thread类的run()方法即可

     线程字段:
    MNX_PRIORITY:          线程可以具有最高优先级
    MIN_PRIORITY:              线程可以具有的最低优先级
    NORM_PRIORITY:       分配给线程的默认优先级
    void join():                     等待线程终止。
    void run() :            如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
     void setName(String name) :   改变线程名称,使之与参数 name 相同。
     void setPriority(int newPriority) : 更改线程的优先级。  
    static void sleep(long millis) :   在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
     void start() :          使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
    static void yield() :        暂停当前正在执行的线程对象,并执行其他线程。
    Boolean isAlive():        测试线程是否处于活动状态
    void interrrupt():        中断线程

    线程对象调用start()方法和调用run()方法的区别
    run():            只有主线程一条执行路径
    start():           多条执行路径,主线程和子线程并行交替执行

    ---继承Thread类创建线程

    示例--

    public class Hello extends Thread{
        
        public void run() 
        {
            for(int i=0;i<10;i++) 
            {
                System.out.println(i+"."+"你好,来自线程"+Thread.currentThread().getName());
            }
        }
    }
    public class MyThread {
    
        public static void main(String[] args) {
            
            Hello thread1=new Hello();        
            thread1.start();
            Hello thread2=new Hello();
            thread2.start();
        }
    
    }

    --实现Runnable接口创建线程

    示例---

    public class Hello implements Runnable{
    
        @Override
        public void run() {
            
            for(int i=0;i<10;i++) 
            {
                System.out.println(i+"."+"你好,来自线程"+Thread.currentThread().getName());
            }
        }
    public class MyRunnable {
    
        public static void main(String[] args) {
            
            Hello hello=new Hello();
            Thread thread1=new Thread(hello);
            thread1.start();
            Thread thread2=new Thread(hello);
            thread2.start();
        }
    
    }

    ----线程的运行状态

    --线程的休眠

    示例--

    public class ThreadDemo {
        
        public void bySec(long s) {
            for (int i = 1; i <=s; i++) {
                System.out.println(i+"秒");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
            }
        }
    }
    public class Wait {
    
        public static void main(String[] args) {
            ThreadDemo t=new ThreadDemo();
            System.out.println("Wait");
            t.bySec(5);
            System.out.println("start");
        }
    
    }

    ---线程的强制运行

    示例--

    public class MyThread implements Runnable{
    
        @Override
        public void run() {
            for(int i=1;i<=10;i++) 
            {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"运行"+i);
            }
        }
    
    }
    public class ThreadJoinDemo {
    
        public static void main(String[] args) {
            System.out.println("线程强制执行");
            
            Thread t=new Thread(new MyThread());
            t.start();
            for(int i=1;i<=10;i++) {
                if(i==5) {
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                    
                }
                System.out.println(Thread.currentThread().getName()+"运行"+i);
            }
        }
    
    }

    --线程的礼让

    示例--

    public class MyThread implements Runnable{
    
        @Override
        public void run() {
            for(int i=1;i<=5;i++) {
                System.out.println(Thread.currentThread().getName()+"正在运行"+i);
                if(i==3) {
                    System.out.println("线程礼让");
                    Thread.yield();
                }
            }
        }
    
    }
    public class ThreadYieldDemo {
    
        public static void main(String[] args) {
            
            Thread t1=new Thread(new MyThread(),"线程A");
            Thread t2=new Thread(new MyThread(),"线程B");
            t1.start();
            t2.start();
        }
    
    }

    线程的同步实现

    示例--

    public class Printer {
    
        public synchronized void printer1() 
        {
            System.out.print("j");
            System.out.print("a");
            System.out.print("v");
            System.out.println("a");
        }
        
        public synchronized void printer2() 
        {
            System.out.print("编");
            System.out.println("程");
        }
    }
    public class MyThread1 implements Runnable{
    
        public Printer printer;
        
        @Override
        public void run() {
            for(int i=1;i<=20;i++)
            {
                printer.printer1();
            }
            
        }
    
    }
    public class MyThread2 implements Runnable{
    
        public Printer printer;
        
        @Override
        public void run() {
                    
                for(int i=1;i<=20;i++)
                {
                    printer.printer2();
                }
        }
    
    }
    public class MyMain {
    
        public static void main(String[] args) {
            MyThread1 m1=new MyThread1();
            Thread t1=new Thread(m1);
            
            MyThread2 m2=new MyThread2();
            Thread t2=new Thread(m2);
            
            Printer printer=new Printer();
            m1.printer=printer;
            m2.printer=printer;
            t1.start();
            t2.start();
            
        }
    
    }

    通过在方法声明中加入synchronized关键词来声明同步方法

    访问修饰符 synchronized 返回类型 方法名 (参数类型){//方法体}

    或者

    synchronized 访问修饰符 返回类型  方法名 (参数类型){//方法体}

  • 相关阅读:
    Servlet概述
    JAVA WEB开发环境与搭建
    Java scirpt简介
    用HTML+CSS编写一个计科院网站首页的静态网页
    CSS样式
    HTML简介
    Web服务器的原理
    静态网页与动态网页的区别
    debugger工具的使用以及调试
    javascript页面刷新的几种方法
  • 原文地址:https://www.cnblogs.com/matianpeng/p/8544334.html
Copyright © 2011-2022 走看看