![]() |
|
|
Регистрация Восстановить пароль |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
![]() |
|
Опции темы | Поиск в этой теме |
![]() |
#1 |
Новичок
Джуниор
Регистрация: 18.05.2011
Сообщений: 1
|
![]()
Привет!
Вот написал программу и теперь не могу ответить на два вопроса... При запуске программу, появляется такой результат: Thread 2 stopped after 1.55 sec Number is now = 5 Thread 3 stopped after 1.62 sec Number is now = 6 Thread 1 stopped after 1.66 sec Number is now = 7 1. Объясните пожалуйста, как мог появится такой результат? 2. Результат будет всегда одинаковым? Почему? Вот код программы: Класс Wheeloffortune public class Wheeloffortune { // instance variables public int ONE=1; public int TWO=2; public int THREE=3; public int FOUR=4; private int number; // constructor public Wheeloffortune() { number=ONE; } // get number public int getNumber() { return number; } // set number public void setNumber(int n) { number=n; } } Класс Fortunethread import java.text.*; public class Fortunethread extends Thread { // instance variables boolean running; Wheeloffortune fortune; int id; long starttime; long endtime; DecimalFormat fmt=new DecimalFormat("###0.00"); // constructor public Fortunethread(Wheeloffortune fortune, int id) { this.fortune=fortune; this.id=id; starttime=System.currentTimeMillis( ); running=true; } // run public void run() { while(running) { if(fortune.getNumber()==fortune.FOU R) { fortune.setNumber(fortune.ONE); } else { fortune.setNumber(fortune.getNumber ()+1); if(fortune.getNumber()>fortune.FOUR ) { endtime=System.currentTimeMillis(); System.out.println("Thread "+id+" stopped after "+fmt.format(0.001*(endtime-starttime))+" sec"); System.out.println("number is now = "+fortune.getNumber()); stopThread(); } } } } // stop public void stopThread() { running=false; } } Класс Testfortune public class Testfortune { // main public static void main(String[] args) { Wheeloffortune wof=new Wheeloffortune(); Fortunethread a=new Fortunethread(wof,1); Fortunethread b=new Fortunethread(wof,2); Fortunethread c=new Fortunethread(wof,3); a.start(); b.start(); c.start(); } } |
![]() |
![]() |
![]() |
![]() |
||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
Потоки | RedScreenOfLife | Общие вопросы C/C++ | 5 | 07.03.2011 14:30 |
Потоки | StudeHt | Помощь студентам | 1 | 06.05.2010 16:26 |
потоки | tanek | Помощь студентам | 6 | 15.03.2010 21:42 |
потоки | Владимир1988 | Помощь студентам | 0 | 08.02.2010 23:12 |