zoukankan      html  css  js  c++  java
  • java多线程

    1、实现多线程:继承Thread类;

    2、实现Runnable接口,并且把该类当作参数传入Thread类或其子类的构造函数中;

    例1:

     1  class ThreadA{
     2   public void run(){
     3     for(int i = 0;i < 100:i++){
     4       System.out.println("ThreadA-->" + i);
     5     }
     6    }
     7  }
     8  
     9  class Test{
    10   public static void main(String[] args){
    11      for(int i = 0;i < 100;i++){
    12        System.out.println("Main-->" + i);
    13      }
    14     ThreadA ta = new ThreadA();
    15     ta.start();
    16    }
    17  }
     例2:
    class RunnableImpl implements Runnable{
      public void run(){
        for(int i = 0;i < 100;i++){
          System.out.println("Runnable-->" + i);
        }
      }
    }
    
    class Test{
      public static void main(String[] args){
        for(int i = 0;i < 100;i++){
          System.out.println("main-->" + i);
        }
        RunnableImpl ri = new RunnableImpl();    
        Thread th
    = new Thread(ri);
        th.start();   
      }
    }
    
    
    
     
  • 相关阅读:
    20165312 我期望的师生关系
    zookeeper04---ZAB协议
    zookeeper03-集群搭建
    zookeeper02
    Zookeeper01
    防止重复提交
    手动抛出异常事务回滚问题
    redis-07主从复制
    redis06-事务
    Redis-05持久化
  • 原文地址:https://www.cnblogs.com/zengneng/p/5597011.html
Copyright © 2011-2022 走看看