- public class LogTest {
- /**
- * 需求:程序代码模拟产生16个日志对象,并且需要16秒才能打印完成日志,请程序添加四个线程,去调用parseLog的方法分头打印,只运行4秒打印完。
- * @param args
- */
- public static void main(String[] args) {
- <span style="color:#ff0000">//新建一个队列
- final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);//1和16都可以
- //建立一个缓冲线程池
- ExecutorService threadPool = Executors.newCachedThreadPool();
- //创建4个Runnable即能产生4个线程
- for (int i = 0; i < 4; i++) {
- threadPool.execute(new Runnable() {
- @Override
- public void run() {
- while(true){
- try {
- //queue.take()获取队列的数据
- String log = queue.take();
- //输出日志
- LogTest.parselog(log);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- });
- }</span>
- System.out.println("begin " + (System.currentTimeMillis() / 1000));
- for (int i = 1; i <= 16; i++) {
- final String log = "" + i;
- //LogTest.parselog(log);
- try {
- <span style="color:#ff0000">//队列放入数据
- queue.put(log);</span>
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 每隔一秒打印一次日志
- * @param log
- */
- public static void parselog(String log) {
- System.out.println(log + ": " + System.currentTimeMillis() / 1000);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
第二题:现成程序中的Test类中的代码在不断地产生数据,然后交给TestDo.doSome()方法去处理,就好像生产者在不断地产生数据,消费者在不断消费数据。请将程序改造成有10个线程来消费生成者产生的数据,这些消费者都调用TestDo.doSome()方法去进行处理,故每个消费者都需要一秒才能处理完,程序应保证这些消费者线程依次有序地消费数据,只有上一个消费者消费完后,下一个消费者才能消费数据,下一个消费者是谁都可以,但要保证这些消费者线程拿到的数据是有顺序的。
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.SynchronousQueue;
- public class Test {
- public static void main(String[] args) {
- <span style="color:#ff0000">//用灯控制一秒钟一个
- <span style="white-space:pre"> </span>final Semaphore semaphore = new Semaphore(1);
- <span style="white-space:pre"> </span>//同步队列
- <span style="white-space:pre"> </span>final SynchronousQueue<String> queue =new SynchronousQueue<String>();
- <span style="white-space:pre"> </span>ExecutorService threadPool = Executors.newCachedThreadPool();
- <span style="white-space:pre"> </span>//十个线程
- <span style="white-space:pre"> </span>for (int i = 0; i < 10; i++) {
- <span style="white-space:pre"> </span>threadPool.execute(new Runnable() {
- <span style="white-space:pre"> </span>@Override
- <span style="white-space:pre"> </span>public void run() {
- <span style="white-space:pre"> </span>try {
- <span style="white-space:pre"> </span>semaphore.acquire();//抢到等的使用权,用Lock也可以
- <span style="white-space:pre"> </span>//从队列里面获取数据,放入TestDo.doSome(input)
- <span style="white-space:pre"> </span>String input = queue.take();
- <span style="white-space:pre"> </span>String output = TestDo.doSome(input);
- <span style="white-space:pre"> </span>System.out.println(Thread.currentThread().getName()+ ":" + output);
- <span style="white-space:pre"> </span>semaphore.release();//用完之后释放掉
- <span style="white-space:pre"> </span>} catch (InterruptedException e) {
- <span style="white-space:pre"> </span>e.printStackTrace();
- <span style="white-space:pre"> </span>}
- <span style="white-space:pre"> </span>}
- <span style="white-space:pre"> </span>});
- <span style="white-space:pre"> </span>}</span>
- System.out.println("begin:"+(System.currentTimeMillis()/1000));
- for(int i=0;i<10;i++){ //这行不能改动
- String input = i+""; //这行不能改动
- <span style="color:#ff0000">//把数据放入队里里面
- try {
- queue.put(input);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }</span>
- }
- }
- }
- //不能改动此TestDo类
- class TestDo {
- public static String doSome(String input){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- String output = input + ":"+ (System.currentTimeMillis() / 1000);
- return output;
- }
- }