多线程
编写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
定义Account
类
属性:private int balance
方法:
getter方法void deposit(int money)
//存钱,在余额的基础上加上moneyvoid withdraw(int money)
//取钱,在余额的基础上减去money
注意:可能有多个线程通过deposit
或withdraw
方法同时存取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;
}
}
}
现已有Account
类,拥有
属性:private int balance
方法:
相应的getter方法。
要求为该类编写:void deposit(int money)
//存钱,在余额的基础上加上moneyvoid withdraw(int money)
//取钱,在余额的基础上减去money
注意:
balance<0
的时候,会抛出异常。在多线程情况下,如只有一个存钱的线程,但是有多个取钱的线程,很可能会抛出异常。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;
}
有一连串任务,需要两个线程交替执行。线程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对象中获取任务。
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()){}
}
}
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 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();
编写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