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

    package Map;

    public class test {
    public static void main(String args[]){
    /* new TestThread().start();*/
    /* TestThread t =new TestThread();
    new Thread(t).start(); */ /*由于Runnable只有Run方法,所以必须调用Thread的start方法*/
    /*for(int i=0;i<10;i++)
    {
    System.out.println("main 线程在运行"+i);
    } */
    /*启动了4个线程,各自使用自己的资源,没有达到资源共享的目的*/
    /*new TestThread().start();
    new TestThread().start();
    new TestThread().start();
    new TestThread().start(); */
    TestThread t = new TestThread();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    }

    }
    class TestThread implements Runnable
    {
    private int tickets = 20;
    public void run()
    {
    while(true)
    {
    if(tickets>0)
    {
    System.out.println(Thread.currentThread().getName()+"出售票"+tickets--);
    }
    }
    }
    }
    /*
    class TestThread extends Thread
    {
    private int tickets = 20;
    public void run()
    {
    while(true)
    {
    if(tickets>0)
    {
    System.out.println(Thread.currentThread().getName()+"出售票"+tickets--);
    }
    }
    }
    }*/
    /*通过Runnable接口实现多线程*/
    /*class TestThread implements Runnable
    {
    public void run()
    {
    for(int i=0;i<10;i++)
    {
    System.out.println("TestThread 在运行。"+i);
    }
    }
    }
    */
    /*
    class TestThread extends Thread
    {
    public void run()
    {
    for(int i=0;i<10;i++)
    {
    System.out.println("TestThread 在运行。"+i);
    }
    }
    }
    */

  • 相关阅读:
    【CodeForces】835D Palindromic characteristics
    【BZOJ】2006: [NOI2010]超级钢琴
    【比赛】STSRM 09
    【比赛】洛谷夏令营NOIP模拟赛
    【BZOJ】4147: [AMPPZ2014]Euclidean Nim
    【BZOJ】3895: 取石子
    【胡策08】解题报告
    【codevs】3196 黄金宝藏
    【BZOJ】1443: [JSOI2009]游戏Game
    【BZOJ】3105: [cqoi2013]新Nim游戏
  • 原文地址:https://www.cnblogs.com/batman425/p/4063158.html
Copyright © 2011-2022 走看看