[每日一练] Java多线程
2014-04-11 16:25
375 查看
Java控制台程序 modified at 2014年4月15日8:54:55
题目1:有100个线程,每个线程对变量 j 加 1,所有线程结束后,输出 j 的值(结果为100)。
提示:
1)对 j 的修改函数或者变量 j 进行同步(synchronized)处理。
2)所有线程都放入ThreadGroup线程组,threadGroup.activeCount>0检测所有线程是否完结。
难度:中
题目2:在题目1的基础上,利用这100个线程,每个线程对变量 j 减 1 ,所有线程结束后,输出 j 的值(结果为0)。
难度:中
题目3:有一个账号,初始余额为 0 ,有两个线程,
1)线程1对账号存入随机钱,线程2从账号取出随机钱,
2)当账号余额不足时,线程2等待线程1存钱,
3)每一次存钱、取钱操作输出余额或者余额不足信息。
提示:需要用到资源的同步(synchronized)、线程的等待(wait)和通知(notify)。
难度:难
题目4:有四个线程,其中两个对变量 j 加一,另外两个对变量 j 减一。
难度:中
题目5:用Runnable接口和Thread基类分别
1)实现一个线程,能够传递参数
2)定义线程名称,在控制台输出名称和参数
3)放入ThreadGroup,当两个线程结束后,输出提示信息
难度:易
题目6:用Java语言描述出Thread的生命周期。
提示:创建、启动、运行、阻塞(时间片、wait、中断)
难度:易
PS:该类练习题,保证线程正确,并发的效率难以保证。
题目1:有100个线程,每个线程对变量 j 加 1,所有线程结束后,输出 j 的值(结果为100)。
提示:
1)对 j 的修改函数或者变量 j 进行同步(synchronized)处理。
2)所有线程都放入ThreadGroup线程组,threadGroup.activeCount>0检测所有线程是否完结。
难度:中
题目2:在题目1的基础上,利用这100个线程,每个线程对变量 j 减 1 ,所有线程结束后,输出 j 的值(结果为0)。
难度:中
public class AccountThread { Account account = new Account(0); Thread[] thread = new Thread[100]; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub AccountThread at = new AccountThread(); } public AccountThread(){ ThreadGroup g = new ThreadGroup("thread group"); System.out.println("Thread\tOperate\t\tBalance"); for(int i=0;i<100;i++){ thread[i] = new Thread(g,new MyThread(1),"thread"+i); thread[i].start(); } while(g.activeCount()!=0){ } System.out.println("after deposit:\t\t"+this.account.getBalance()); for(int i=0;i<100;i++){ thread[i] = new Thread(g,new MyThread(-1),"thread"+i); thread[i].start(); } while(g.activeCount()!=0){ } System.out.println("after withdraw:\t\t"+this.account.getBalance()); } class MyThread extends Thread{ int amount = 0; public MyThread(int a){ amount = a; } @Override public void run(){ account.deposit(amount); } } class Account{ int _balance; public Account(int balance){ _balance = balance; } public int getBalance(){ return _balance; } public void deposit(int amount){ synchronized((Object)_balance){ _balance += amount; System.out.println(Thread.currentThread().getName() + " deposit " + amount+"\t\t"+getBalance()); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
题目3:有一个账号,初始余额为 0 ,有两个线程,
1)线程1对账号存入随机钱,线程2从账号取出随机钱,
2)当账号余额不足时,线程2等待线程1存钱,
3)每一次存钱、取钱操作输出余额或者余额不足信息。
提示:需要用到资源的同步(synchronized)、线程的等待(wait)和通知(notify)。
难度:难
package com.threadexercise; public class AccountThread { Account account = new Account(100); boolean done = false; boolean over = false; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub AccountThread at = new AccountThread(); System.out.println("Thread1\t\tThread2\t\tBalance"); } public AccountThread(){ ThreadGroup g = new ThreadGroup("group"); DepositThread dt = new DepositThread(); dt.start(); WithdrawThread wt = new WithdrawThread(); wt.start(); } class DepositThread extends Thread{ @Override public void run(){ while(true){ int n = (int)(Math.random()*10)+1; account.deposit(n); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class WithdrawThread extends Thread{ @Override public void run(){ while(true){ int n = (int)(Math.random()*10)+1; account.withdraw(n); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class Account{ public Account(int amount){ balance = amount; } int balance; public int getBalance(){ return balance; } public synchronized void deposit(int amount){ balance+=amount; System.out.println("deposit: "+amount+"\t\t\t\t"+account.getBalance()); notifyAll(); } public synchronized void withdraw(int amount){ while(balance < amount){ System.out.println(balance +" < " + amount + " not enough"); try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } balance -= amount; System.out.println("\t\twithdraw: "+amount+"\t\t"+account.getBalance()); } } }
题目4:有四个线程,其中两个对变量 j 加一,另外两个对变量 j 减一。
难度:中
package com.ThreadExercise; public class SimpleThread { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Thread\t\tamount\t\tJ"); SimpleThread st = new SimpleThread(); } public SimpleThread(){ int number; for(int i=0;i<2;i++){ number = (int)(Math.random())+1; Thread t1 = new Thread(new MyThread(1)); t1.setName("Thread"+i); t1.start(); } for(int i=0;i<2;i++){ number = (int)(Math.random())+1; Thread t1 = new Thread(new MyThread(-1)); t1.setName("Thread"+(i+2)); t1.start(); } //number = (int)(Math.random()*10)+1; Thread control = new Thread(new Thread(new ControlThread(30000))); control.start(); } int j = 0; boolean mark = true; public synchronized void operateJ(int amount){ j += amount; System.out.println(Thread.currentThread().getName()+"\t\t"+amount+"\t\t"+j); } class ControlThread implements Runnable{ int time; public ControlThread(int amount){ this.time = amount; } @Override public void run(){ try { System.out.println("after "+time+" seconds stop!"); Thread.sleep(time); mark = false; System.out.println("threads stopped!"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class MyThread implements Runnable{ int amount; public MyThread(int num){ amount = num; } @Override public void run(){ while(mark){ operateJ(amount); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
题目5:用Runnable接口和Thread基类分别
1)实现一个线程,能够传递参数
2)定义线程名称,在控制台输出名称和参数
3)放入ThreadGroup,当两个线程结束后,输出提示信息
难度:易
题目6:用Java语言描述出Thread的生命周期。
提示:创建、启动、运行、阻塞(时间片、wait、中断)
难度:易
PS:该类练习题,保证线程正确,并发的效率难以保证。