zoukankan      html  css  js  c++  java
  • Java SE (6)之 多线程

    package com.sunzhiyan03;
    /*
     * 演示多线程
     * */
    public class Demo3 {
    
    	public Demo3() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Pig pig = new  Pig();
    		Bird bd = new Bird(10);
    		//启动一个线程
    		Thread pig_run = new Thread(pig);
    		Thread bd_run = new Thread(bd);
    		pig_run.start();
    		bd_run.start();
    	}
    
    }
    
    class Bird implements Runnable{
    	int n = 0;
    	int res = 0;
    	int times = 0;
    	public Bird(int n){
    		this.n = n;
    	}
    	public void run(){
    
    	 while(true){
    		try {
    			Thread.sleep(1000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		res += (++times);
    		System.out.println("结果是"+res);
    		if(times == n){
    			break;
    		}
    	  }
    	}	
    }
    class Pig implements Runnable{
    	int n = 0;
    	int times = 0;
    	public void run(){
    		while(true)
    		{
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			System.out.println("我是一个线程");
    			times ++;
    			if(times == 10){
    				break;
    			}
    		}		
    	}
    	
    }
    

      

  • 相关阅读:
    C#---将数据库数据转换为json格式
    ASP.NET ---根据值让树中某一节点选中
    SQL---查询树中某个节点及其所有子节点
    CSS---相对定位笔记
    CSS---绝对定位笔记
    滑雪
    Self Numbers
    Lotto
    Parencodings
    Robot Motion
  • 原文地址:https://www.cnblogs.com/sunxun/p/3841341.html
Copyright © 2011-2022 走看看