zoukankan      html  css  js  c++  java
  • 线程学习的几个实例

    1.售票系统

    public class DemoTest {
        public static void main(String args[]){
            ThreadTest mtTest= new ThreadTest();
            new Thread(mtTest).start();
            new Thread(mtTest).start();
            new Thread(mtTest).start();
        }
        
        public boolean isNumber(String str){
            char[] no=str.toCharArray();
            for(int i=0;i<no.length;i++){
                if(!(no[i]>='0' && no[i]<='9')){
                    return false;
                }
            }
            return true;
        }
    }
    
    class ThreadTest implements Runnable{
        int tickets=100;
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                saleTicket();
            }
        }
        
        public synchronized void saleTicket(){
            synchronized (this) {
                if(tickets>0){
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName()+"----"+"ticket"+"--->"+tickets--);
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }
    }

    2.存钱

    public class ThreadTest_1 {
        public static void main(String args[]){
            ThreadDemo mtDemo=new ThreadDemo();
            new Thread(mtDemo,"小明").start();  //两个人去存款,一共存了6次,共600元
            new Thread(mtDemo,"小红").start();
        }
    }
    
    
    class ThreadDemo implements Runnable{
        int account=0;
        @Override   //每个人存3次钱,一次存100
        public void run() {
            // TODO Auto-generated method stub
            for(int i=0;i<10;i++){
                saveMoney(100);
            }
        }
        
        
        public synchronized void saveMoney(int money){
            account = account+money; 
            try {
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName()+"存入100元后,现在账户余额为"+account);
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }

    3.火车过山洞

    public class ThreadTest_2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ThreadDemo_1 mtDemo=new ThreadDemo_1();
            new Thread(mtDemo,"火车A").start();
            new Thread(mtDemo,"火车B").start();
            new Thread(mtDemo,"火车C").start();
            new Thread(mtDemo,"火车D").start();
            new Thread(mtDemo,"火车E").start();
        }
    }
    
    class ThreadDemo_1 implements Runnable{
        @Override
        public void run() {
            
            // TODO Auto-generated method stub
                throughHill();
        }
        
        public synchronized void throughHill(){
            try {
                Thread.sleep(1000);
                System.out.println("此时穿过山洞的火车是:"+Thread.currentThread().getName());
            } catch (InterruptedException e) {
                // TODO: handle exception
            }
        }
    }
  • 相关阅读:
    PAT甲级——1110 Complete Binary Tree (完全二叉树)
    PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
    PAT甲级——1112 Stucked Keyboard (字符串+stl)
    PAT甲级——1114 Family Property (并查集)
    PAT甲级——1118 Birds in Forest (并查集)
    Pre- and Post-order Traversals(先序+后序序列,建立二叉树)
    PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)
    AVL树(自平衡二叉查找树)
    PAT甲级——1126 Eulerian Path
    团队项目-选题报告
  • 原文地址:https://www.cnblogs.com/redick/p/6381057.html
Copyright © 2011-2022 走看看