zoukankan      html  css  js  c++  java
  • Java多线程(一) Thread和 Runnable

    http://www.cnblogs.com/lwbqqyumidi/p/3804883.html

    1.继承Thread

    2.实现Runnable接口

       

    public class MyRunnable implements Runnable {
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
          System.out.println("running..");
        }
    
    }
    public class Run {
        public static void main(String[] args) {
            Runnable runnable =new MyRunnable();
            Thread thread = new Thread(runnable);
            thread.start();
        }
    
    }

      如果同时继承Thread类实现Runnable接口,实际执行的Thread的run方法

    public class ThreadTest {
    
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
                if (i == 30) {
                    Runnable myRunnable = new MyRunnable();
                    Thread thread = new MyThread(myRunnable);
                    thread.start();
                }
            }
        }
    }
    
    class MyRunnable implements Runnable {
        private int i = 0;
    
        @Override
        public void run() {
            System.out.println("in MyRunnable run");
            for (i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
        }
    }
    
    class MyThread extends Thread {
    
        private int i = 0;
    
        public MyThread(Runnable runnable){
            super(runnable);
        }
    
        @Override
        public void run() {
            System.out.println("in MyThread run");
            for (i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
        }
    }
  • 相关阅读:
    07 selenium模块基本使用
    06 高性能异步爬虫
    05 request高级操作
    04 验证码识别
    03 数据解析
    02 requests模块
    01 爬虫简介
    Get和Post的正解
    pytoch之 encoder,decoder
    pytorch之 RNN 参数解释
  • 原文地址:https://www.cnblogs.com/newlangwen/p/7551922.html
Copyright © 2011-2022 走看看