Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NewThread extends Thread {
- private String name;
- private Thread thread;
- private boolean suspendFlag;
- NewThread(String name) {
- this.name = name;
- this.thread = new Thread(this, name);
- this.suspendFlag = false;
- this.thread.start();
- }
- public void run() {
- try {
- for (int i = 0; i < 10; ++i) {
- System.out.println(name + " => ЗДРАВО!");
- Thread.sleep(1000);
- synchronized (this) {
- while (suspendFlag) {
- this.wait();
- }
- }
- }
- } catch (InterruptedException e) {
- System.err.println(e);
- }
- System.out.println(name + " => ЗАВРШИВ!");
- }public void mySuspend() {
- this.suspendFlag = true;
- }
- public synchronized void myResume() {
- this.suspendFlag = false;
- this.notify();
- }
- public static void main(String args[]) {
- NewThread thread1 = new NewThread("Нишка 1");
- NewThread thread2 = new NewThread("Нишка 2");
- NewThread thread3 = new NewThread("Нишка 3");
- try {
- Thread.sleep(3000);
- System.out.println("Ја стопираме нишката 1!");
- thread1.mySuspend();
- System.out.println("Ја стопираме нишката 2!");
- thread2.mySuspend();
- Thread.sleep(6000);
- System.out.println("Нишката 1 продолжи со работа!");
- thread1.myResume();
- System.out.println("Нишката 2 продолжи со работа!");
- thread2.myResume();
- System.out.println("Ја стопираме нишката 3!");
- thread3.mySuspend();
- Thread.sleep(3000);
- System.out.println("Нишката 3 продолжи со работа!");
- thread3.myResume();
- thread1.join();
- thread2.join();
- thread3.join();
- } catch (InterruptedException e) {
- System.err.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement