java多线程创建方式一Thread

      jdk1.5之前利用的线程建立体例。

      1、建立利用担当Thread类并重写run方式的体例实现,或者利用匿名内部类的体例。

  生命周期的五种状况 新建(new Thread) 当建立Thread类的一个实例(对象)时,此线程进入新建状况(未被启动)。 例如:Thread  t1=new Thread(); 停当(runnable) 线程已经被启动,正在期待被分派给CPU时候片,也就是说此时线程正在停当队列中列队等待获得CPU资本。例如:t1.start(); 运行(running) 线程获得CPU资本正在执行使命(run()方式),此时除非此线程主动抛却CPU资本或者有优先级更高的线程进入,线程将一向运行到竣事。 灭亡(dead) 当线程执行完毕或被其它线程杀死,线程就进入灭亡状况,这时线程不成能再进入停当状况期待执行。 天然终止:正常运行run()方式后终止 异常终止:挪用stop()方式让一个线程终止运行 堵塞(blocked) 因为某种原因导致正在运行的线程让出CPU并暂停本身的执行,即进入堵塞状况。 正在睡眠:用sleep(long t) 方式可使线程进入睡眠体例。一个睡眠着的线程在指心猿意马的时候曩昔可进入停当状况。 正在期待:挪用wait()方式。(挪用motify()方式回到停当状况) 被另一个线程所梗阻:挪用suspend()方式。(挪用resume()方式恢复)

东西/原料

  • 电脑
  • intellij IDEA

方式/步调

  1. 1

    根基利用

    package com.sgg.thread;


    /**

     * 多线程的建立。体例1担当与Thread类

     * 1.建立一个担当与Thread的子类

     * 2.重写run方式,将此线程执行的操出声明在run方式中

     * 3.建立Thread子类对象

     * 4.经由过程此对象挪用start()方式启动线程

     *

     */

    public class TestThread {



        public static void main(String[] args) {

            //ConcurrentHashMap<Object, Object> objectObjectConcurrentHashMap = new ConcurrentHashMap<Object, Object>();


            MyThread myThread = new MyThread();

            myThread.start();



            //利用匿名体例担当

    //        new Thread(){

    //

    //            @Override

    //            public void run() {

    //                for (int i = 0; i < 100; i++) {

    //                    if (i%2 == 1 ){

    //                        System.out.println(Thread.currentThread().getName()+":"+i);

    //                    }

    //

    //                    if(i%20 == 0){

    //                        //释放CPU的执行权

    //                        this.yield();

    //                    }

    //

    //                }

    //            }

    //        }.start();



            for (int i = 0; i < 100; i++) {

                if (i%2 == 1 ){

                    System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":"+i);

                }


                if( i== 10){

                    try {

                        myThread.join();

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                }


            }


            System.out.println("myThread是否存活:"+myThread.isAlive());


        }

    }



    //利用集当作体例

    class MyThread extends Thread{


        @Override

        public void run() {


            for (int i = 0; i < 100; i++) {

                if (i%2 ==0 ){


                    try {

                        sleep(1000);

                    } catch (InterruptedException e) {

                        e.printStackTrace();

                    }

                    System.out.println(Thread.currentThread().getName()+":"+i);

                }


            }

        }



        public MyThread(){};


        //建立时设置线程名字

        public MyThread(String name){

            //挪用java.lang.Thread.Thread(java.lang.String)

            super(name);

        }

    }

  2. 2

    常用方式界说

    package com.sgg.thread;


    /**

     * 测试Thread类中常用的方式

     * 1.strat() :启动当火线程,并挪用当火线程的run()

     * 2.run(): 凡是需要从头Thread中的此方式,将建立的线程中所需的操作(也就是营业代码)声明在此方式中

     * 3.currentThread(): 静态方式,返回当前代码的线程

     * 4.getName():获取当火线程的名字

     * 5.setName():设置当火线程房名字

     * 6.yield() :是否当火线程CPU的执行权

     * 7.join() :在线程a中挪用线程b的join(),此时线程a就进入梗阻状况,知道线程b执行完之后,线程a才竣事梗阻状况

     * 8.stop() :已过时,当执行此方式时,强制竣事当火线程。

     * 9.sleep(long millitime) :让当火线程“睡眠”指心猿意马的millitime毫秒,在指心猿意马的millitime毫秒时候内,当火线程是梗阻状况

     * 10.isAlive() :判定当火线程是否存活

     *

     *

     * 线程的优先级

     * 1.MAX_PRIORITY = 10;

     * 2.NORM_PRIORITY = 5;

     * 3.MIN_PRIORITY = 1;

     *若何获取和设置当火线程的优先级

     * 1.getPriority()

     * 2.setPriority(int newPriority) :启动之前设置线程的优先级

     *

     * 申明:高优先级的线程要抢占低优先级线程CPU的执行权。可是只是从概率上讲,高优先级的线程高概率下被执行。并不料味着高优先级线程执行无缺,低优先级的线程才会执行。

     */

    public class ThreadMethodTest {


        public static void main(String[] args) {


        }

    }

  3. 3

    实例操练


    package com.sgg.thread;


    class MyThread extends Thread {


    private static int ticket = 1000;


    @Override

    public void run() {


    while (true) {

    if (!show()) {

    break;

    }

    ;

    }

    }

    private static synchronized boolean show() {//同步监督器:MyThread.class

    //private synchronized boolean show() {//同步监督器就是:this  别离是myt1,myt2,myt3  此处利用是错误的

    if (ticket > 0) {

    try {

    Thread.sleep(100);

    } catch (InterruptedException e) {

    e.printStackTrace();

    }


    System.out.println(Thread.currentThread().getName() + "票号:" + ticket);

    ticket--;

    return true;

    }

    return false;

    }


    }


    /**

     * 利用同步代码块处置担当Thread类的线程平安问题

     * 例子 :建立三个窗口卖票,票总数100 利用实现Runable的体例

     * 申明:在担当Thread类建立多线程的体例中,慎用this充任同步监督器,考虑利用当前类充任监督器如MyThread.class

     * @author Administrator

     *

     */

    public class WindowsThread {

    public static void main(String[] args) {

    MyThread myt1 = new MyThread();

    myt1.start();

    MyThread myt2 = new MyThread();

    myt2.start();

    MyThread myt3 = new MyThread();

    myt3.start();

    //ConcurrentHashMap<K, V>

    }


    }

  4. 4

    线程平安问题

    1、利用lock锁

    2、利用synchronized关头字,同步方式或者同步块

  • 发表于 2019-08-13 22:56
  • 阅读 ( 792 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

0 篇文章

作家榜 »

  1. xiaonan123 189 文章
  2. 汤依妹儿 97 文章
  3. luogf229 46 文章
  4. jy02406749 45 文章
  5. 小凡 34 文章
  6. Daisy萌 32 文章
  7. 我的QQ3117863681 24 文章
  8. 华志健 23 文章

联系我们:uytrv@hotmail.com 问答工具