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

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

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

3

输出样例:

0
1
2
标识信息
import java.util.Scanner;
/*这里放置你的答案,即MyThread类的代码*/
class MyThread extends Thread{
    private int n;
    public MyThread(int a){n=a;}
    public void run(){
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName()+" "+isAlive());
    }
}
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();
    }
}

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

定义Account类
属性:
private int balance
方法:
getter方法
void deposit(int money) //存钱,在余额的基础上加上money
void withdraw(int money) //取钱,在余额的基础上减去money
注意:可能有多个线程通过deposit或withdraw方法同时存取Account对象的balance属性。

import java.util.Scanner;

/*你的代码,即Account类的代码*/
class Account{
    private int balance;
    public Account(){balance=0;}
    public Account(int a){balance=a;}
    public int getBalance(){return balance;}
    public synchronized void deposit(int a){balance+=a;}
    public synchronized void withdraw(int a){balance-=a;}
}
/*系统已有代码,无需关注*/

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

现已有Account类,拥有
属性:

private int balance

方法:
相应的getter方法。
要求为该类编写:

void deposit(int money) //存钱,在余额的基础上加上money
void withdraw(int money) //取钱,在余额的基础上减去money

注意:
取钱时如果balance<0的时候,会抛出异常。在多线程情况下,如只有一个存钱的线程,但是有多个取钱的线程,很可能会抛出异常。
需要编写完整的deposit方法与withdraw的前半部分代码解决该问题。
输入样例:
分别为初始余额、存钱次数、存钱金额
取钱次数、取钱金额。有3个线程。

0 100000 12
100000 4

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

0
0
import java.util.Scanner;

//这里是已有的Account类前半部分的代码
/*这里是deposit代码*/
/*这里是withdraw代码的前半部分*/
public synchronized void deposit(int a){balance+=a;}
public synchronized void withdraw(int a){
    if(balance>=a)balance-=a;
    if(balance<0) //这里是withdraw代码的后半部分。
        throw new IllegalStateException(balance+"");    
    }

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

6-4jmu-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包含的任务数量。注意:完成任务的时候,需要将任务删除。
//其他完成任务的方法
定义Worker1与Worker2类,代表两个交替完成任务的类,可以从Repo对象中获取任务。
输入样例

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

类对象传进去的是地址,所以操作的时候会对原对象进行操作,因此才能实现这个功能

import java.util.*;
class Repo{
    ArrayList<String> list=new ArrayList<String>();
    int len;
    int mutex=0;
    public Repo(String items){
        String []s=items.split("\\s+");
        len=s.length;
        for(int i=0;i<len;i++){
            list.add(s[i]);
        }
    }
    public int getSize(){
        return len;
    }
    public String getTask(){
        String res=list.get(0);
        list.remove(0);
        len--;
        return res;
    }
}
class Worker1 implements Runnable{
    Repo rp;
    public Worker1(Repo a){
        rp=a;
    }
    public void run(){
        String task="";
        while(rp.getSize()>0){
            synchronized(rp){
                if(rp.getSize()>0&&rp.mutex==0){
                    task=rp.getTask();
                    rp.mutex=1;
                    System.out.println(Thread.currentThread().getName()+" finish "+task);
                }
            }
        }
    }
}
class Worker2 implements Runnable{
    Repo rp;
    public Worker2(Repo a){
        rp=a;
    }
    public void run(){
        String task="";
        while(rp.getSize()>0){
            synchronized(rp){
                if(rp.getSize()>0&&rp.mutex==1){
                    task=rp.getTask();
                    rp.mutex=0;
                    System.out.println(Thread.currentThread().getName()+" finish "+task);
                }
            }
        }
    }
}
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();
    }
}

6-5jmu-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();
    }
}

答案Collections.synchronizedList(new ArrayList<>());

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

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

3

输出样例:

0
1
2
标识信息
import java.util.Scanner;
/*这里放置你的答案,即PrintTask类的代码*/
class PrintTask implements Runnable{
    int n;
    public PrintTask(int a){n=a;}
    public void run(){
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName());
    }
}
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();
    }
}

8-1 生产者和消费者问题

编写程序,实现生产者和消费者问题。输入:由英文字母和数字组成的不定长度字符串数组,例如{“abc”,”23d”,”1a”}。每隔100毫秒,生产者线程读入数据,放入生产者公用的仓库;消费者从仓库中取出字符串,进行倒置,如上例{“cba”,”d32”,”a1”}; 输出:倒置后的字符串。

提示:可以定义4个类:生产者类、消费者类、产品类和测试类。

import java.util.*;
class Product{
	String []data;//原字符串数组
	private String aString=null;//存放目前生产者生产以及消费者要拿的字符串
	private boolean f=false;//标识该生产者线程还是消费者线程
	public Product(String[] s) {data=s;}
	public int getDataLength() {
		return data.length;
	}
	public synchronized void put(String a) {
		if(f==true) {
			try {
				wait();
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		aString=a;
		f=true;
		notify();
	}
	public synchronized void get() {
		if(f==false) {
			try {
				wait();
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if(aString!=null) {//倒序输出
			for(int i=aString.length()-1;i>=0;i--) {
				System.out.print(aString.charAt(i));
			}
			System.out.println();
		}
		f=false;
		notify();
	}
}
class Producer implements Runnable{
	private Product p=null;
	public Producer(Product p) {
		this.p=p;
	}
	public void run() {
		int i=0;
		while(true) {
			try { //循环从字符串数组中拿取字符串并生产
				i=i%p.getDataLength();
				p.put(p.data[i]);
				Thread.sleep(100);
			}catch (InterruptedException e) {
				e.printStackTrace();
			}
			i++;
		}
	}
}
class Consumer implements Runnable{
	private Product p=null;
	public Consumer(Product p) {
		this.p=p;
	}
	public void run() {
		while(true) {
			p.get();
		}
	}
}
public class CommunicationTest {
	public static void main(String []args) {
		Scanner in=new Scanner(System.in);
		String str=in.nextLine();
		String []s = str.split("\\s+");
		Product p=new Product(s);
		new Thread(new Producer(p)).start();
		new Thread(new Consumer(p)).start();
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值