• java多线程


    package a;

    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;

    public class Copy extends JFrame implements ActionListener,Runnable

    {

        int value;

        JProgressBar progressBar1,progressBar;

        JButton button1,button2;

        Label label;

        Thread thread;

        public Copy()

        {

            super("文件复制进度条演示");

            this.setSize(700,400);

            this.setLocationRelativeTo(null);

            this.setDefaultCloseOperation(EXIT_ON_CLOSE);

            this.setLayout(new GridLayout(4,3));

            JPanel cmdpanel1=new JPanel();

            JPanel cmdpanel2=new JPanel();

            this.add(cmdpanel1);

            this.add(cmdpanel2);

            cmdpanel1.add(this.button1=new JButton("复制文件"));

            cmdpanel1.add(this.button2=new JButton("取消"));

            this.button2.setEnabled(false);

            this.button1.addActionListener(this);

            this.button2.addActionListener(this);

            cmdpanel2.add(this.label=new Label("                                    "));

            this.label.setVisible(true);

            this.add(this.progressBar = new JProgressBar());

            progressBar.setForeground(Color.blue);

            progressBar.setStringPainted(true);

            progressBar.setValue(0);

            this.thread=new Thread(this);

            this.setVisible(true);

        }

        public void actionPerformed(ActionEvent event)

        {

            if(event.getSource()==this.button1)

            {

                this.thread.start();

                this.button1.setEnabled(false);

                this.button2.setEnabled(true);

                this.label.setVisible(true);

            }

            if(event.getSource()==this.button2)

            {

                this.thread.interrupt();

                this.label.setText("文本复制已完成0%");

                updateProgressBar(0);

                this.button1.setEnabled(true);

                this.button2.setEnabled(false);

            }

        }

        public void run()

        {

            while(true)

            {

                try

                {

                    value=progressBar.getValue()+1;

                    this.label.setText("文本复制已完成"+progressBar.getValue()+"%");

                    Thread.sleep(10);

                    updateProgressBar(value);

                    if(progressBar.getValue()==100)

                    {

                       this.label.setText("文件复制已完成!");

                       break;

                    }

                }

                catch(InterruptedException ex)

                {

                    break;

                }

            }

        }

        private void updateProgressBar(int value)

        {

            progressBar.setValue(value);

        }

        public static void main(String[] args)

        {

            new Copy();

        }

    }

    package a;

    public class Bank

    {

        Depositor account;

        public void put(Depositor account,double value)

        {

            this.account=account;

            if(value>0)

            this.account.put(this.account,value);

        }

        public static void main(String[] args)

        {

            Depositor W=new Depositor("Wang");

            Depositor L=new Depositor("Li");

            Depositor Z=new Depositor("Zhang");

            (new SaveThread(W,L,100)).start();

            (new SaveThread(L,L,100)).start();

            (new SaveThread(Z,L,100)).start();

        }

    }

    package a;

    public class Depositor

    {

        String name;

        double balance;

        Depositor account;

        Depositor(String name)

        {

            this.name=name;

            this.balance=0;

        }

        public void put(Depositor account,double value)

        {

            this.account=account;

            this.account.balance+=value;

            System.out.println("存入"+value+",余额"+this.account.balance());

            this.account.balance+=value;

            System.out.println("存入"+value+",余额"+this.account.balance());

            this.account.balance+=value;

            System.out.println("存入"+value+",余额"+this.account.balance());

        }

        public String getName()

        {

            return this.name;

        }

        public double balance()

        {

            return this.balance;

        }

    }

    package a;

    public class SaveThread extends Thread

    {

        private Depositor account1,account2;

        Bank bank=new Bank();

        private double value;

        public SaveThread(Depositor account1,Depositor account2,double value)

        {

            this.account1=account1;

            this.account2=account2;

            this.value=value;

        }

        public void run()

        {

            synchronized(this.account2)

            {

                try

                {

                    Thread.sleep(1);

                }

                catch(InterruptedException ex) {}

                System.out.println(this.account1.getName()+"账户:向"+this.account2.getName()+"账户存款");

                bank.put(this.account2,this.value);

            }

        }

    }

     

     

  • 相关阅读:
    AngularJS中service,factory,provider的区别
    AngularJs数据绑定原理
    H5项目常见问题及注意事项
    本地存储-localStroage/sessionStorage存储
    微信小程序 获取OpenId
    Nginx 常用命令
    Redis 分布式锁 解决集群环境下多次定时任务执行
    Spring Boot 动态修改 Scheduled (系统启动默认执行,动态修改)
    SpringBoot 部署war包
    Docker + Tomcat 实现 Springboot 项目增量升级
  • 原文地址:https://www.cnblogs.com/s98z1226/p/11946311.html
Copyright © 2020-2023  润新知