数字加减
题目:设计四个线程对象,两个线程执行加操作,两个线程执行减操作。
class Resource{//定义一个操作的对象
private int num=0;//这个是要进行加减的数据
private boolean flag=true;//加减的切换 flag=true执行加法 flag=false执行减法
public synchronized void add()throws Exception{//执行加法操作
if(this.flag==false){
super.wait();
}
Thread.sleep(100);
this.num++;
System.out.println("加法操作、"+Thread.currentThread().getName()+"、num="+this.num);
this.flag=false;
super.notifyAll();
}
public synchronized void sub() throws Exception{//执行减法操作
if(this.flag==true){
super.wait();
}
Thread.sleep(200);
this.num--;
System.out.println("减法操作、"+Thread.currentThread().getName()+"、num"+this.num);
this.flag=true;
super.notifyAll();
}
}
class AddThread implements Runnable{
private Resource resource;
public AddThread(Resource resource){
this.resource=resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.add();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class SubThread implements Runnable{
private Resource resource;
public SubThread(Resource resource){
this.resource=resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.sub();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class ThreadCase {
public static void main(String[] args) {
Resource res = new Resource();
AddThread addThread = new AddThread(res);
SubThread subThread=new SubThread(res);
new Thread(addThread,"加法线程A").start();
new Thread(addThread,"加法线程B").start();
new Thread(subThread,"减法线程A").start();
new Thread(subThread,"减法线程B").start();
}
}
这个程序的核心在于加一个、减一个,整体的计算结果应该只出现0、1、-1之间循环出现
生产电脑
设计一个生产电脑和搬运电脑类,要求生产出一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑产出,如果生产的电脑没有被搬走,则要等待电脑搬走之后在生产,并统计出生产的电脑数量。
class Computer{
private static int count=0;//表示生产的个数
private String name;
private double price;
public Computer(String name,double price){
this.name=name;
this.price=price;
count++;
}
public String toString(){
return "【第"+count+"台电脑、"+"电脑名字:"+this.name+"、价值"+price;
}
}
class Resource{
private Computer computer;
public synchronized void make() throws InterruptedException {
if(this.computer!=null){
super.wait();
}
Thread.sleep(100);
this.computer=new Computer("Dell",1000);
System.out.println("【生产电脑】"+this.computer);
super.notifyAll();
}
public synchronized void get() throws InterruptedException {
if(this.computer==null){
super.wait();
}
Thread.sleep(10);
System.out.println("【取出电脑】"+this.computer);
this.computer=null;
super.notifyAll();
}
}
class ProducerComputer implements Runnable{
private Resource resource;
public ProducerComputer(Resource resource){
this.resource=resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.make();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class GetComputer implements Runnable{
private Resource resource;
public GetComputer(Resource resource){
this.resource=resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.get();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadCase {
public static void main(String[] args) {
Resource res = new Resource();
new Thread(new ProducerComputer(res)).start();
new Thread (new GetComputer(res)).start();
}
}
竞争抢答
实现一个竞拍抢答程序,要求设置三个抢答者线程,而后同时发出抢答指令,抢答成功者给出成功提示,抢答未成功者给出失败提示。
对于这一个多线程的操作,由于里面牵涉到数据的返回问题,那么现在最好使用是Callable是比较方便的处理形式。
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class MyyThread implements Callable<String> {
private boolean flag=false;//抢答处理
@Override
public String call() throws Exception {
if(!this.flag){
this.flag=true;
return Thread.currentThread().getName()+"抢答成功";
}else{
return Thread.currentThread().getName()+"抢答失败";
}
}
}
public class ThreadCase {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyyThread myyThread = new MyyThread();
FutureTask<String> futureTask = new FutureTask(myyThread);
Thread taskA = new Thread(futureTask, "抢答者A");
Thread taskB = new Thread(futureTask, "抢答者B");
Thread taskC = new Thread(futureTask, "抢答者C");
taskA.start();
taskB.start();
taskC.start();
System.out.println(futureTask.get());
}
}