2022-3-30作业

Cloud_vv 2022-03-30 21:17:38

1、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果。


public class HWDemo01 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 2; i < 100000; i++) {
                    boolean flag = true;
                    for (int j = 2; j < i; j++) {
                        if (i % j == 0) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        System.out.print(i + " ");
                    }
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 100000; i < 200000; i++) {
                    boolean flag = true;
                    for (int j = 2; j < i; j++) {
                        if (i % j == 0) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        System.out.print(i + " ");
                    }
                }
            }
        };
        t1.start();t2.start();
        t1.join();t2.join();
    }
}


2、模拟多线程并发问题,并解决(方式越多越好)

public class Account {
    private int balance;//余额

    public Account(int balance) {
        this.balance = balance;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }
}

方式1:public class AccountThread {
    public static void main(String[] args) throws InterruptedException {
        Account account = new Account(1000);
        Thread t1 = new Thread(){
            @Override
            public void run() {
                int balance = account.getBalance();
                System.out.println("正在出钞...");
                balance -= 200;
                Thread.yield();//设置线程的状态  为  就绪状态
                //修改余额
                account.setBalance(balance);
                System.out.println(Thread.currentThread().getName() +
                        "取了 200 余额:" + account.getBalance());
            }
        };

        Thread t2 = new Thread(){
            @Override
            public void run() {
                try {
                    t1.join();//写在t2线程的run中,会让t2等待t1运行结束之后再运行(串行)
                    int balance = account.getBalance();
                    System.out.println("正在出钞...");
                    balance -= 200;
                    Thread.yield();//设置线程的状态  为  就绪状态
                    //修改余额
                    account.setBalance(balance);
                    System.out.println(Thread.currentThread().getName() +
                            "取了 200 余额:" + account.getBalance());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        //让线程t1执行完毕取款操作后,再让线程t2执行即可,将线程的并发操作改为串行操作。
        t1.start();  t2.start();
        //下面两行代码  写在main线程中,会使得main线程去等待t1  t2的结束
        t1.join();   t2.join();
        System.out.println(Thread.currentThread().getName() + ":"+account.getBalance());


    }
}

方式2:public class AccountThread02 {
    public static void main(String[] args) throws InterruptedException {
        Account account = new Account(1000);
        Demo demo = new Demo();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                synchronized (AccountThread02.class){
                    //需要保证原子性的代码  需要加锁的代码
                    int balance = account.getBalance();
                    System.out.println("正在出钞...");
                    balance -= 200;
                    Thread.yield();//设置线程的状态  为  就绪状态
                    //修改余额
                    account.setBalance(balance);
                    System.out.println(Thread.currentThread().getName() +
                            "取了 200 余额:" + account.getBalance());
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                synchronized (AccountThread02.class){
                    int balance = account.getBalance();
                    System.out.println("正在出钞...");
                    balance -= 200;
                    Thread.yield();//设置线程的状态  为  就绪状态
                    //修改余额
                    account.setBalance(balance);
                    System.out.println(Thread.currentThread().getName() +
                            "取了 200 余额:" + account.getBalance());
                }
            }
        };
        //让线程t1执行完毕取款操作后,再让线程t2执行即可,将线程的并发操作改为串行操作。
        t1.start();  t2.start();
        //下面两行代码  写在main线程中,会使得main线程去等待t1  t2的结束
        t1.join();   t2.join();
        System.out.println(Thread.currentThread().getName() + ":"+account.getBalance());


    }
}

class  Demo {}


方式3:public class SynchronizedDemo {
    public static void main(String[] args) throws InterruptedException {
        Account account = new Account(1000);
        Demo demo = new Demo();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                synchronized (account){
                    //需要保证原子性的代码  需要加锁的代码
                    int balance = account.getBalance();
                    System.out.println("正在出钞...");
                    balance -= 200;
                    Thread.yield();//设置线程的状态  为  就绪状态
                    //修改余额
                    account.setBalance(balance);
                    System.out.println(Thread.currentThread().getName() +
                            "取了 200 余额:" + account.getBalance());
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                synchronized (demo){
                    int balance = account.getBalance();
                    System.out.println("正在出钞...");
                    balance -= 200;
                    Thread.yield();//设置线程的状态  为  就绪状态
                    //修改余额
                    account.setBalance(balance);
                    System.out.println(Thread.currentThread().getName() +
                            "取了 200 余额:" + account.getBalance());
                }
            }
        };
        //让线程t1执行完毕取款操作后,再让线程t2执行即可,将线程的并发操作改为串行操作。
        t1.start();
        t2.start();
        //下面两行代码  写在main线程中,会使得main线程去等待t1  t2的结束
        t1.join();
        t2.join();
        System.out.println(Thread.currentThread().getName() + ":" + account.getBalance());


    }
}

3.模拟两个人(两个线程),同时在桌子上拿豆子,考虑并发出错情况


public class Beans {
    private int num ;
    public Beans(int num) {
        this.num = num;
    }
    public  void catchBeans()  {
        if (num == 0){
            throw new RuntimeException("豆子没了");
        }
        int n = num;
        n = n - 1;
        Thread.yield();
        num = n;

    }

    public  int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

}

public class BeansTest {
    public static void main(String[] args) {
        Beans beans = new Beans(100);
        Thread t1 = new Thread() {
            @Override
            public void run() {
                while (true) {
                    beans.catchBeans();
                    Thread.yield();
                    System.out.println(beans.getNum());

                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                while (true) {
                    beans.catchBeans();
                    Thread.yield();
                    System.out.println(beans.getNum());
                }
            }
        };
        t1.start();
        t2.start();

    }
}

...全文
248 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

7,476

社区成员

发帖
与我相关
我的任务
社区描述
新人大本营,在这个社区你可以见证自己的成长。
其他 其他
社区管理员
  • community_284
  • CSDN官方博客
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

进入本社区请遵循以下规则:

  • 禁止在社区里发广告
  • 禁止在社区讨论违法的话题

试试用AI创作助手写篇文章吧