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();
        }
        
    }
  • 相关阅读:
    线程应用:(九)阻塞队列的应用
    线程应用:(八)Semaphere信号灯、CyclicBarrier汇合、CountDownLatch计数器、Exchanger
    线程应用:(七)Lock & Condition同步通信
    线程应用:(六)线程池、Callable与Future
    线程应用:(五)传统线程使用实例
    记一次linux安装mysql
    Pycharm断点调试入门
    django admin后台插件:django-suit入门
    在django中使用django_debug_toolbar
    pyquery详细用法
  • 原文地址:https://www.cnblogs.com/exexex/p/8419096.html
Copyright © 2011-2022 走看看