Java程序设计2023-第六次上机练习

多线程

6-1 jmu-Java-07多线程-Thread

编写MyThread类继承自Thread。创建MyThread类对象时可指定循环次数n。
功能:输出从0到n-1的整数。 并在最后使用System.out.println(Thread.currentThread().getName()+" "+isAlive())打印标识信息

裁判测试程序:

import java.util.Scanner; /*这里放置你的答案,即MyThread类的代码*/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Thread t1 = new MyThread(Integer.parseInt(sc.next())); t1.start(); } }

输入样例:

3

输出样例:

0
1
2
标识信息

 代码



/*这里放置你的答案,即MyThread类的代码*/
class MyThread extends Thread{
    int n;
    public MyThread(int _n){
        this.n = _n;
    }
    public void run(){
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName()+" "+isAlive());
    }
}

6-2 jmu-Java-07多线程-互斥访问 

定义Account
属性:
private int balance
方法:
getter方法
void deposit(int money) //存钱,在余额的基础上加上money
void withdraw(int money) //取钱,在余额的基础上减去money

注意:可能有多个线程通过depositwithdraw方法同时存取Account对象的balance属性。

裁判测试程序:

import java.util.Scanner; /*你的代码,即Account类的代码*/ /*系统已有代码,无需关注*/ 

代码 

在方法中使用synchronized(this)来实现互斥访问



class Account extends Thread{
    private int balance;
    private int flag = 0;

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

    public  void deposit(int money){
        synchronized(this){
            this.balance += money;       
        }
    }

    public  void withdraw(int money){
        synchronized(this){
            this.balance -= money;       
        }
    }
}

6-3 jmu-Java-07多线程-同步访问 

现已有Account类,拥有
属性:
private int balance
方法:
相应的getter方法。

要求为该类编写:
void deposit(int money) //存钱,在余额的基础上加上money
void withdraw(int money) //取钱,在余额的基础上减去money

注意:

  1. 取钱时如果balance<0的时候,会抛出异常。在多线程情况下,如只有一个存钱的线程,但是有多个取钱的线程,很可能会抛出异常。
  2. 需要编写完整的deposit方法与withdraw的前半部分代码解决该问题。

裁判测试程序:

import java.util.Scanner; //这里是已有的Account类前半部分的代码 /*这里是deposit代码*/ /*这里是withdraw代码的前半部分*/ if(balance<0) //这里是withdraw代码的后半部分。 throw new IllegalStateException(balance+""); } /*系统已有代码,无需关注*/ 

输入样例:

分别为初始余额、存钱次数、存钱金额
取钱次数、取钱金额。有3个线程。

0 100000 12
100000 4

输出样例:

余额:使用线程跑出的结果。
余额:存钱次数*存钱金额 - 取钱次数*取钱金额*3

0
0

代码

使用wait()和notifyAll()来实现同步访问


    public void deposit(int money){
        synchronized (this) {
            balance += money;       
            notifyAll();
        }
    }

    public void withdraw(int money){
        synchronized (this) {
            while(balance < money){
                try{
                    wait();
                } catch (InterruptedException e){
                }
            }
            balance -= money;       
        }
    

 6-4 jmu-Java-07多线程-交替执行

有一连串任务,需要两个线程交替执行。线程1执行完任务1后,线程2才能执行任务2,接下来线程1执行任务1,如此交替执行下去。直到所有任务执行完毕。

定义Repo类代表任务仓库,使用字符串代表任务。该类拥有:
构造方法:

/*将传递进来的字符串以空格分隔分解为多个不同的任务,并存储起来。如"1 2 3 4 5 6"被分解成6个任务1,2,3,4,5,6*/
public Repo(String items) {
}

方法:

int getSize();  //返回Repo包含的任务数量。注意:完成任务的时候,需要将任务删除。
//其他完成任务的方法

定义Worker1Worker2类,代表两个交替完成任务的类,可以从Repo对象中获取任务。

main方法如下:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Scanner sc = new Scanner(System.in);
        Repo repo = new Repo(sc.nextLine());
        Thread t1 = new Thread(new Worker1(repo));
        Thread t2 = new Thread(new Worker2(repo));
        t1.start();
        Thread.yield();
        t2.start();
        sc.close();
    }
}

输入样例

1 2 3 4 5 6 7 8 9

输出样例

Thread-0 finish 1
Thread-1 finish 2
Thread-0 finish 3
Thread-1 finish 4
Thread-0 finish 5
Thread-1 finish 6
Thread-0 finish 7
Thread-1 finish 8
Thread-0 finish 9

裁判测试程序:

/*Repo代码*/ /*Worker1代码*/ /*Worker2代码*/ /*系统已有代码,无需关注*/

 代码

import java.util.*;

class Repo{
    private String[] s;
    int id = 0;
    public Repo(String items){
        s = items.split(" ");
    }

    public int getSize(){
        return s.length;
    }

    public synchronized boolean work(){
        this.notify();
        // if(getSize() < 0){
        //     return false;
        // }
        // else{
            String ts = s[id++]; 
            System.out.println(Thread.currentThread().getName() + " finish " + ts);
            try{
                this.wait();
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        //}
        return true;
    }
}


class Worker1 implements Runnable{
    Repo repo;
    public Worker1(Repo repo){
        this.repo = repo;
    }

    public void run(){
        while(repo.work()){} 
    }
}

class Worker2 implements Runnable{
    Repo repo;
    public Worker2(Repo repo){
        this.repo = repo;
    }

    public void run(){
        while(repo.work()){} 
    }
}

6-5 jmu-Java-07多线程-集合同步问题 

ArrayList, LinkedList等都不是线程安全的。多个线程同时在这些对象上进行访问、修改可能会带来同步问题。
尝试解决这个问题。

####裁判程序示例


class SimpleTask implements Runnable {
    private final CountDownLatch latch;
    private static Random rand = new Random(47);
    public SimpleTask(CountDownLatch latch) {
        this.latch = latch;
    }
    @Override
    public void run() {
        try {
            TimeUnit.NANOSECONDS.sleep(rand.nextInt(5));
            Main.list.add((int)(Math.random()*10));
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Main  throws InterruptedException{
    public static List<Integer> list;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Main.list = /*你的答案*/
        /*以下是多个线程频繁在Main.list上增删元素的代码*/
        while (sc.hasNextInt()) {
            int m = sc.nextInt();  
            int poolSize = sc.nextInt();
            CountDownLatch latch = new CountDownLatch(m);
            ExecutorService exec = Executors.newFixedThreadPool(poolSize);
            for (int i = 0; i < m; i++) {
                exec.execute(new SimpleTask(latch));
            }
            exec.shutdown();
            latch.await();
            //这里是一些测试代码,如检验list中元素个数是否正确
            Main.list.clear();
        }
        sc.close();

    }
}

代码

new java.util.Vector<Integer>();

6-6 jmu-Java-07多线程-PrintTask 

 

编写PrintTask类实现Runnable接口。
功能:输出从0到n-1的整数(n在创建PrintTask对象的时候指定)。并在最后使用System.out.println(Thread.currentThread().getName());输出标识信息。

裁判测试程序:

import java.util.Scanner; /*这里放置你的答案,即PrintTask类的代码*/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintTask task = new PrintTask(Integer.parseInt(sc.next())); Thread t1 = new Thread(task); t1.start(); sc.close(); } }

输入样例:

3

输出样例:

0
1
2
标识信息

代码

实现Runnable接口并重写run方法

class PrintTask implements Runnable{
    private int n;
    public PrintTask(int _n){
        this.n = _n;
    }
    public void run(){
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯你说的对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值