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);
    }
    }
    }
    */

  • 相关阅读:
    Maven3-依赖
    Maven2-坐标
    使用VS Code开发Python
    WinDbg调试分析 asp.net站点 CPU100%问题
    asp.net core2 Centos上配置守护服务(Supervisor)
    asp.net core2部署到Centos上
    IntelliJ Error:Abnormal build process termination
    EF架构~codeFirst从初始化到数据库迁移
    office web apps 实现Wopi预览编辑
    office web apps安装教程
  • 原文地址:https://www.cnblogs.com/batman425/p/4063158.html
Copyright © 2011-2022 走看看