|
| 1 | +package org.javacore.thread; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright [2015] [Jeff Lee] |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Jeff Lee |
| 21 | + * @since 2015-11-2 16:56:43 |
| 22 | + * Runnable接口的实现类LiftOff |
| 23 | + */ |
| 24 | +public class LiftOff implements Runnable{ |
| 25 | + |
| 26 | + protected int countDown = 10; |
| 27 | + private static int taskCount = 0; |
| 28 | + private final int id = taskCount++; |
| 29 | + |
| 30 | + public LiftOff(){} |
| 31 | + public LiftOff(int countDown){ |
| 32 | + this.countDown = countDown; |
| 33 | + } |
| 34 | + |
| 35 | + public String status(){ |
| 36 | + return "#" + id + "(" + |
| 37 | + (countDown >0 ? countDown : "LiftOff!") + "), "; |
| 38 | + } |
| 39 | + @Override |
| 40 | + public void run() { |
| 41 | + while (countDown-- > 0){ |
| 42 | + System.out.println(status()); |
| 43 | + // 暂停当前正在执行线程,并执行其他线程。 |
| 44 | + Thread.yield(); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments