62,614
社区成员
发帖
与我相关
我的任务
分享
public class Sample09 {
/**
* @param args
*/
public static void main(String[] args) {
// ExecutorService es = Executors.newFixedThreadPool(3);
// ExecutorService es = Executors.newSingleThreadExecutor();
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < 1000; i++) {
Task t = new Task();
es.submit(t);
}
System.out.println("加载任务完成");
es.shutdown();
System.out.println("main is over");
}
}
class Task implements Runnable {
private String name = "";
private static int num = 1;
private static int step = 0;
private long sleepTime = 200;
public Task(String name, long sleepTime) {
this.name = name;
this.sleepTime = sleepTime;
}
public Task() {
this("task-" + num, 200);
num++;
}
public void run() {
int num = 1;
int all = new Random().nextInt(20) + 1;
System.out.println("\n==========" + Thread.currentThread().getName()
+ "开始执行" + name + "任务(该任务有" + all + "步)" + "==========");
step = 0;
while (num <= all) {
System.out.print("执行任务" + name + "第" + num++ + "步 ");
step++;
if (step % 6 == 0) {
System.out.println();
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("\n==========" + Thread.currentThread().getName()
+ "执行完毕==========");
step = 0;
}
}
public class Sample08 extends Thread {
private Object o1 = null;
private Object o2 = null;
public Sample08(Object o1, Object o2) {
this.o1 = o1;
this.o2 = o2;
}
public void run() {
synchronized (o1) {
System.out.println(Thread.currentThread().getName() + "锁住对象" + o1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println(Thread.currentThread().getName() + "锁住对象" + o2);
}
System.out.println(Thread.currentThread().getName() + "释放对象" + o2);
}
System.out.println(Thread.currentThread().getName() + "释放对象" + o1);
}
/**
* @param args
*/
public static void main(String[] args) {
String str1 = "str1";
String str2 = "str2";
String str3 = "str3";
Sample08 t1 = new Sample08(str1, str2);
Sample08 t2 = new Sample08(str2, str3);
Sample08 t3 = new Sample08(str3, str1);
t1.start();
t2.start();
t3.start();
}
}
public class Sample07 {
/**
* @param args
*/
public static void main(String[] args) {
Warehouse wh = new Warehouse();
Product p1 = new Product(wh);
Product p2 = new Product(wh);
Consumer c = new Consumer(wh);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
p1.start();
p2.start();
}
}
class Product extends Thread {
private Warehouse wh = null;
public Product(Warehouse wh) {
this.wh = wh;
}
public void run() {
int num = 1;
while (num < 21) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
wh.push(num++);
}
}
}
class Consumer extends Thread {
private Warehouse wh = null;
public Consumer(Warehouse wh) {
this.wh = wh;
}
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (wh.pop() == 20) {
break;
}
}
}
}
class Warehouse {
private int num = 0;
private boolean isFull = false;
public synchronized void push(int num) {
if (!isFull) {
this.num = num;
System.out.println("生产者" + Thread.currentThread().getName() + "放入--->" + num);
isFull = true;
notify();
}
if (isFull) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized int pop() {
if (isFull) {
System.out.println("消费者获得--->" + num);
isFull = false;
notifyAll();
return num;
}
if (!isFull) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return -1;
}
}