zoukankan      html  css  js  c++  java
  • 学习总结 java线程

     1 package com.hanqi.xc;
     2 
     3 public class Test1 {
     4 
     5     public static void main(String[] args) {
     6 
     7         // 线程测试
     8         for (int i = 0; i < 10; i++) {
     9             System.out.println("i = " + i);
    10 
    11             // 通过线程类,控制线程
    12             try {
    13                 // 让主线程休眠100毫秒
    14                 Thread.sleep(100);
    15 
    16             } catch (InterruptedException e) {
    17                 // TODO 自动生成的 catch 块
    18                 e.printStackTrace();
    19             }
    20 
    21         }
    22 
    23         Test2 t2 = new Test2();
    24 
    25         // 单线程
    26         // t2.test();
    27 
    28         // 启动新线程
    29 
    30         t2.start();
    31 
    32         Test2 t3 = new Test2();
    33         
    34         t3.start();
    35         
    36         Test2 t4 = new Test2();
    37         
    38         t4.start();
    39         
    40         //启动接口方式的分线程
    41         
    42         Thread th = new Thread(new Test3(),"接口线程4");
    43         
    44         th.start();
    45     }
    46 
    47     
    48     
    49     
    50     
    51     
    52     
    53     
    54     
    55 }
    package com.hanqi.xc;
    
    //以继承的方式支持多线程
    public class Test2 extends Thread 
    {
      
        //重写run方法
        
        
        @Override
        public void run ()
        {
            //调应需要并发执行的语句
            test();
            
            
        }
        
        
        
        // 测试方法
        public void test() 
        {
            for (int i = 0; i < 10; i++) 
            {
                System.out.println("i = " + i);
    
                // 通过线程类,控制线程
                try 
                {
                    // 让主线程休眠100毫秒
                    Thread.sleep(100);
    
                } catch (InterruptedException e) 
                {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
    
            }
    
            
        }
    package com.hanqi.xc;
    
    public class Test3 implements Runnable {
    
        @Override
        public void run() {
            
            
            //需要分线程执行的代码
    
            for (int i = 0; i < 10; i++) 
            {
                System.out.println("i = " + i);
    
                // 通过线程类,控制线程
                try 
                {
                    // 让主线程休眠100毫秒
                    Thread.sleep(100);
    
                } catch (InterruptedException e) 
                {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
    
            }
    
        }
    
    }

  • 相关阅读:
    总结对象和数组存储东东的缺点和优点
    this关键字
    修改对象属性的方法
    调用对象属性的两种方式
    数组可储存的东西
    对比字面量和结构函数创建对象的相同之处和不同之处
    构造函数创建对象
    字面量创建对象
    SQL语句
    使用SQL Server 2008的事务日志传送功能备份数据库(logshiping)
  • 原文地址:https://www.cnblogs.com/zhoudi/p/5558171.html
Copyright © 2011-2022 走看看