zoukankan      html  css  js  c++  java
  • JAVA多线程的总结

    1-----------------------------------基本概念-------------------------------------------------

    (1)多线程:一个应用程序有多条执行路径
            进程:正在执行的应用程序
            线程:进程的执行单元,执行路径
            单线程:一个应用程序只有一条执行路径
            多线程:一个应用程序有多条执行路径
            多进程的意义?
                提高CPU的使用率
            多线程的意义?
                提高应用程序的使用率

    (2)多线程的实现方案
            A:继承Thread类
            B:实现Runnable接口

    (3)线程的调度
            A:线程的调度
                a:分时调度
                b:抢占式调度 (Java采用的是该调度方式)

    (4)同步解决线程安全问题
            A:同步代码块
                synchronized(对象) {
                    需要被同步的代码;
                }
                
                这里的锁对象可以是任意对象。
                
            B:同步方法
                把同步加在方法上。
                
                这里的锁对象是this
                
            C:静态同步方法
                把同步加在方法上。
                
                这里的锁对象是当前类的字节码文件对象(反射再讲字节码文件对象)

    (5)线程的生命周期

         

    2-----------------------------------基础代码-------------------------------------------------

    (1)继承Thread

    /**
     * 继承Thread
     * @author Administrator
     *
     */
       public class XC1 extends Thread {

               String bb =null ;
        
               public XC1(){}
        
               public XC1(String aa){
                     super();
                     bb=aa;
        }
        
        @Override
        public void run() {
               for (int i = 0; i < 1000; i++) {
                       System.out.println("线程1:"+1111+bb);
            }
        }
    }
    (2)实现Runnable接口


    public class XC2 implements Runnable {

        
        public void run() {
            for (int i = 0; i < 1000; i++) {
                System.out.println("线程2:"+2222);
            }
        }

    }
    (3)调用


    public class Demo1 {

        public static void main(String[] args) {

            XC1 xc1 = new XC1();
            Thread th1 = new Thread(xc1);
            th1.start();
            XC2 xc2 = new XC2();
            Thread th2 = new Thread(xc2);
            th2.start();

        }

    }

  • 相关阅读:
    索引压缩
    拼写校正
    词典(词汇表)
    Text Relatives II
    Text Relatives
    CoreText
    Quartz2D Text
    PDF Document Creation, Viewing
    Core Graphics Layer Drawing
    Bitmap Images and Image Masks
  • 原文地址:https://www.cnblogs.com/taotingkai/p/6182368.html
Copyright © 2011-2022 走看看