zoukankan      html  css  js  c++  java
  • 线程与进程

    线程:一个进程最少有一个线程,线程是进程的一个执行单元

    进程:具有一定独立功能的程序,一个实体,每个进程都有他自己的地址空间。

    进程状态:运行、执行、阻塞

    线程的两种实现方式:

    1)继承Thread,重写run方法

    2)实现Runnable接口,实现run方法(推荐使用,因为此种方法相当与添加了个任务,而不是创建新的线程,且只能单继承)

    sleep与wait的区别?

    sleep:让线程进入休眠状态,让出cpu的时间片,不释放对象监视器所有权(对象锁)

    wait:让线程进入等待状态,让出cpu的时间 片,并释放对象监视器的所有权,等待其他线程通过notify方法唤醒

    join方法实例:

    public class JoinTest {
        public static void main(String [] args) throws InterruptedException {
            ThreadJoinTest t1 = new ThreadJoinTest("小明");
            ThreadJoinTest t2 = new ThreadJoinTest("小东");
            t1.start();
            /**join方法可以传递参数,join(10)表示main线程会等待t1线程10毫秒,10毫秒过去后,
             * main线程和t1线程之间执行顺序由串行执行变为普通的并行执行
             */
            t1.join(10);
            t2.start();
        }
    
    }
    class ThreadJoinTest extends Thread{
        public ThreadJoinTest(String name){
            super(name);
        }
        @Override
        public void run(){
            for(int i=0;i<1000;i++){
                System.out.println(this.getName() + ":" + i);
            }
        }
    }

    sleep方法及自定义标记中断线程demo(interrupt()中断线程,任何线程中断线程,抛出异常时,线程中断将被清除):

    public class ThreadTest {
        public static void main(String[] args){
            MyThread mt = new MyThread();
            //推荐
            MyRunnable mr = new MyRunnable();
    
            MyRunnable1 mr1 = new MyRunnable1();
            mr1.flag = true;
    
            //Runnable接口本质不是一个线程,相当一个任务,需要把它放到线程中去
            Thread t2 = new Thread(mr);
            Thread t3 = new Thread(mr1);
            //mt.start();
            t2.start();
            t3.start();
            for(int i  = 0 ; i< 20 ;i++){
                //System.out.println(Thread.currentThread().getName()+"-b"+i);
                try {
                    //sleep为静态方法,让当前线程休眠0.5s
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                if(i==10){
                    mr1.flag=false;
                }
            }
    
        }
    }
    
    //线程的第一种实现方式:继承Thread类
    class MyThread extends Thread{
        @Override
        public void run() {
            for(int i = 0; i < 20; i++){
                System.out.println(Thread.currentThread().getName()+"-a"+i);
                try {
                    //sleep为静态方法,让当前线程休眠0.5s
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //线程的第二种实现方式:实现Runnnable接口
    class MyRunnable implements  Runnable{
    
        @Override
        public void run() {
            for(int i = 0; i < 20; i++){
                System.out.println(Thread.currentThread().getName()+"-c"+i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //自定义标记中断线程
    class MyRunnable1 implements  Runnable{
        boolean flag;
        int i = 0;
        public MyRunnable1(){
            flag = true;
        }
        @Override
        public void run() {
            while(flag){
                System.out.println(Thread.currentThread().getName()+"-d"+(i++));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    可以多个广告浮动的页面
    获取内容并截取长度用...替换
    用UIManager使Swing界面转换风格。
    Java FileInputStream
    win7 部署tomcat总结
    观察者模式
    Java RandomAccessFile
    Java 客户端界面功能:停止当前操作
    多线程的开启与管理
    log4j内容记录
  • 原文地址:https://www.cnblogs.com/wenbiquan/p/11121568.html
Copyright © 2011-2022 走看看