zoukankan      html  css  js  c++  java
  • 2016-7-5 Java : Thread

     两种方式:

    1.继承Thread
    
    2.实现Runnable

     1.

    class MyThread extends Thread{
        public static int num = 0;
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(num<100) {
                System.out.println(Thread.currentThread().getName() + " -> "+ num++);
            }
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            MyThread thread01 = new MyThread();
            MyThread thread02 = new MyThread();
            thread01.start();
            thread02.start();
        }
    }

    2.

    class YourThread implements Runnable {
        private static int num = 0;
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (num < 100) {
                System.out.println(Thread.currentThread().getName() + " -> " + num++);
            }
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Thread thread01 = new Thread(new YourThread(),"t01");
            Thread thread02 = new Thread(new YourThread(),"t02");
            thread01.start();
            thread02.start();
        }
    
    }

         

    这种同步问题不会每次都出现.

  • 相关阅读:
    【转】杭电ACM试题分类
    python strip()函数介绍
    正则表达式的语法规则
    POSIX扩展正则表达式函数
    JS学习笔记
    PCRE兼容正则表达式函数
    vs 2008 feature pack 之体验
    #单元测试
    300道四则运算题
    观后感
  • 原文地址:https://www.cnblogs.com/juzi-123/p/5644682.html
Copyright © 2011-2022 走看看