
本文共 4830 字,大约阅读时间需要 16 分钟。
< html > < div > < head > < title > ��������������������������������������������� </ title > </ head > < div > < body > < h2 > ��������������������������������������������� </ h2 >
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������ thread ��������������� runnable ��������������� Java ��� Thread ��������������������������������� runnable ������������������������ thread ������������������������������������������ runnable ��������������������� thread ������������������������������������������������ runnable ������������������������������������������ thread������������������������ thread ������������������������ runnable ������������������������������������
��������������������������������������������������������� thread ��������������������������������������� Race ������������������ Thread��������������� runnable ������������������������������������������������������������ instance of Race������������������ thread ������������������������������ runnable ���������������������
������������������������������������
package thread.demo1;public class Race implements Runnable { private static String winner; @Override public void run() { boolean flag; for (int i = 1; i <= 1000; i++) { if (Thread.currentThread().getName().equals("������") && i % 5 == 0) { try { System.out.println("������������"); Thread.sleep(1); i += 5; } catch (InterruptedException e) { e.printStackTrace(); } } flag = gameOver(i); if (flag) { //��������������������������������������� break; } System.out.println(Thread.currentThread().getName() + "������" + i + "���"); } } public boolean gameOver(int dist) { if (winner != null) { return true; } else { if (dist >= 1000) { winner = Thread.currentThread().getName(); System.out.println("winner is " + winner); return true; } return false; } } public static void main(String[] args) { Race race = new Race(); new Thread(race, "������").start(); new Thread(race, "������").start(); }}
������������������������������������������������������������
������������������������������ runnable ��������������������� thread ��������������� thread ������������������������ runnable ������������������������������������������������������������������������ thread ���������������������������������
������������������������������������������������ thread ��������������������� runnable ������������������������������������������������������������������������������
������������������������������������ thread ������ 1000 ��������������������������������������������������������������������������� thread ������������������������������������������������������
���������������������Java ��� Thread ���������������������������������������������������������������������������������������������������������
������������������������������������������������������������
��������������� runnable ��������������� runnable ��������������������������������������������������������� thread ���������������������������������
��������synchronized ��������������������������������������������������������������������������� synchronized ������������������������������ thread ������������������ runnable ������������������
������ sleeping ��������������������� thread.sleep() ���������������������������������������������������������������
������ gameOver ������������ setting winner ��������������������������� loop ���������������������������������������������������
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������� runnable ��������������������� thread ������������������������������������������������������������������������������������������������
��������������������������������������������������������������������������������������������������������� thread pools��������������������������������� thread������������������ runnable ������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
