zoukankan      html  css  js  c++  java
  • (CSDN迁移)JAVA多线程实现-继承Thread

    1. 继承Thread方法:
    extends Thread
    
    1. 重写覆盖run()方法:
    @Override
    public void run()
    
    1. 通过start()方法启动线程。
    threadDemo01.start();
    
    1. 若需要向线程中传递参数,可以采用在线程类(如例子中的ExtendThread)定义成员变量,成员变量可以是基本类型,也可以是其他类,例如,可以在run方法中回调成员变量的方法。
    public class ExtendThread extends Thread{
    	
    	public static int ii = 0;
    	public MyPrint = new MyPrint()
    	@Override
        public void run(){
            //编写自己的线程代码
    		for (int i = 0; i < 10; i++) {
    			System.out.println(Thread.currentThread().getName() + ": i = " + ii++);
    			try {
    				sleep(Math.round(Math.random()*100));
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
        }
    	
    	
        public static void main(String[] args){ 
        	ExtendThread threadDemo01 = new ExtendThread();
        	ExtendThread threadDemo02 = new ExtendThread(); 
            threadDemo01.setName("线程1");
            threadDemo02.setName("线程2");
            threadDemo01.start();     
            threadDemo02.start();
        }
    }
    

    更简便的实现方式则是直接在插入线程的地方添加代码:(并不推荐)

    final String string = ...;
    final int ddd = ...;
    new Thread(){
    	public void run() {
                 //TODO in the new Thread
    	};
    }.start();
    
  • 相关阅读:
    8-2蒙版初识
    8-1使用自由变换(有些操作和教程不同)
    7-11使用色彩调整图层
    7-10使用历史记录画笔
    7-9将灰度转为彩色
    7-8其他色彩调整
    7-7自动色阶/自动对比度/自动颜色
    7-6替换颜色和色彩范围选取
    7-5匹配颜色
    7-4暗调/高光
  • 原文地址:https://www.cnblogs.com/AbelZone/p/10064125.html
Copyright © 2011-2022 走看看