zoukankan      html  css  js  c++  java
  • 11,多线程示例代码

    实例1

    package 多线程;
    /*
     * 多窗口售票
     * 此程序为错误程序,每张票买了四次
     * */
    class ticket extends Thread{
        private int ticket=100;
        public void run()
        {
            while(true)
            {
                if(ticket>0)
                {    
                    System.out.println("sales:"+ticket--);
                }
            }
        }
        
    }
    public class tick {
        public static void main(String[] args)
        {
            ticket t0=new ticket();
            ticket t1=new ticket();
            ticket t2=new ticket();
            ticket t3=new ticket();
            t0.start();    
            t1.start();
            t2.start();
            t3.start();
        }
        
    
    
    }

    示例代码2

    解决多线程共享变量问题:声明实现runnable接口的类

    解决打印0,-1,-2票:使用同步代码块

    package 多线程;
    /*
     * 多窗口售票
     * implements Runnable
     * */
    class ticket implements  Runnable{
        private int ticket=100;
        public void run()
        {
            while(true)
            {
                synchronized (this) {
                    if(ticket>0)
                    {    try{Thread.sleep(10);}catch(Exception e){}
                        System.out.println(Thread.currentThread().getName()+"sales:"+ticket--);
                    }
                }
            }
        }
        
    }
    public class tick {
        public static void main(String[] args)
        {        
            ticket t=new ticket();
            Thread Th0=new Thread(t);
            Thread Th1=new Thread(t);
            Thread Th2=new Thread(t);
            Thread Th3=new Thread(t);
            Th0.start();    
            Th1.start();
            Th2.start();
            Th3.start();
        }
        
    
    
    }

    同步代码函数

    package song.yan;
    
    class Bank{
        private int sum;
        //同步代码块
        /*public void add(int n)
        {
            synchronized(this)
            {
                sum=sum+100;
                try{Thread.sleep(100);}catch(Exception e){}
                System.out.println("sum="+sum);        
            }        
        }*/
        //同步函数
        public synchronized void add(int n)
        {
            sum=sum+100;
            try{Thread.sleep(100);}catch(Exception e){}
            System.out.println("sum="+sum);        
            
        }
    }
    class Demos implements Runnable{
        private Bank b= new Bank();
        
        public void run()
        {
            for(int i=0;i<3;i++)
            {
                b.add(100);
            }
        }
    }
    public class tick{
        public static void main(String[] args) {
            Demos d=new Demos();
            Thread t1=new Thread(d);
            Thread t2= new Thread(d);
            t1.start();
            t2.start();
        }
        
    }

    //

    package song.yan.com;
    /*
     * 将同步设在run()
     * 将变成单线程问题
     * 
     * */
    
    class Ticks implements Runnable{
        private int num=1000;
        
        public synchronized  void run()
        {
            while(true)
            {
                if(num>0)
                {
                    System.out.println(Thread.currentThread().getName()+"num="+num--);
                }            
            }
            
        }
    }
    public class tick{
        public static void main(String[] args) {
            Ticks d=new Ticks();
            Thread t1=new Thread(d);
            Thread t2= new Thread(d);
            Thread t3=new Thread(d);
            Thread t4= new Thread(d);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
        
    }

    正确使用同步函数:将需要同步的部分单独写在synconized函数中,在run函数中调用

    package song.yan.com;
    /*
     * 将同步设在run()
     * 将变成单线程问题
     * 
     * */
    
    class Ticks implements Runnable{
        private int num=2000;
        
        public   void run()
        {
            while(true)
            {
                show();            
            }
            
        }
        public synchronized void show()
        {
            if(num>0)
            {
                System.out.println(Thread.currentThread().getName()+"num="+num--);
            }
        }
    }
    public class tick{
        public static void main(String[] args) {
            Ticks d=new Ticks();
            Thread t1=new Thread(d);
            Thread t2= new Thread(d);
            Thread t3=new Thread(d);
            Thread t4= new Thread(d);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
        
    }
  • 相关阅读:
    idea设置全局ignore
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please ins
    win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistr
    kafka 删除 topic
    java编译中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError
    Centos中使用yum安装java时,没有jps的问题的解决
    Spring 整合Junit
    Spring纯注解配置
    Spring 基于注解的 IOC 配置
    打印java系统的信息
  • 原文地址:https://www.cnblogs.com/exexex/p/8419096.html
Copyright © 2011-2022 走看看