zoukankan      html  css  js  c++  java
  • 多线程篇一:传统线程实现方式

    1.new 一个Thread子类(这里是一个内部类)通常写法如SubThread

    Thread thread=new Thread(){
     @Override
     public void run(){
          while(true){
              try {
                    Thread.sleep(500);
                    System.out.println("0:"+Thread.currentThread());
                    System.out.println("1:"+this.currentThread());
                   } catch (InterruptedException e) {
                        e.printStackTrace();
                   }
               }
           }
     }; 
    thread.start();

    SubThread

    package com.test.thread;
    
    public class SubThread extends Thread{
    
        @Override
        public void run(){
            while(true){
                System.out.println("111111111111");
            }
        }
        
        public static void main(String[] args) {
            SubThread sub=new SubThread();
            sub.start();
        }
    }

    2.new一个Runnable对象 ,面向对象的使用方式

    Thread thread2=new Thread(new Runnable(){
                @Override
                public void run(){
                    while(true){
                        try {
                            Thread.sleep(500);
                            System.out.println("2:"+Thread.currentThread());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
      thread2.start();

    方法2通常写法如下

    Thread thread3=new Thread(new SubRunable());
    thread3.start();

    package com.test.thread;
    
    public class SubRunable implements Runnable{
    
        @Override
        public void run() {
            while(true){
                try {
                    Thread.sleep(500);
                    System.out.println("SubRunable:"+Thread.currentThread());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  • 相关阅读:
    html5 canvas 小例子 旋转的时钟
    用深度学习(CNN RNN Attention)解决大规模文本分类问题
    生成式对抗网络GAN 的研究进展与展望
    linux 系统信息查看
    cmd 更改字体
    查看sbt版本
    机器学习算法汇总
    spark 大数据 LR测试
    spark
    hadoop生态圈介绍
  • 原文地址:https://www.cnblogs.com/brant/p/5958114.html
Copyright © 2011-2022 走看看