Advertisement
jaVer404

level16.lesson13.home01

Sep 13th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package com.javarush.test.level16.lesson13.home01;
  2.  
  3. /* Thread.currentThread - всегда возвращает текущую нить
  4.  
  5. 1. В методе printMsg присвой переменной t текущую нить.
  6. 2. В методе printMsg после всех действий поставь задержку в 1 миллисекунду.
  7. */
  8.  
  9. public class Solution {
  10.     static int count = 5;
  11.  
  12.     public static void main(String[] args) {
  13.         NameOfDefferentThreads tt = new NameOfDefferentThreads();
  14.         tt.start();
  15.         for (int i = 0; i < count; i++) {
  16.             tt.printMsg();
  17.         }
  18.     }
  19.  
  20.     public static class NameOfDefferentThreads extends Thread {
  21.         public void run() {
  22.             for (int i = 0; i < count; i++) {
  23.                 printMsg();
  24.             }
  25.         }
  26.         public void printMsg() {
  27.             try {
  28.             Thread t = Thread.currentThread();//присвой переменной t текущую нить
  29.             String name = t.getName();
  30.             System.out.println("name=" + name);
  31.             sleep(1); }
  32.             catch (InterruptedException e) {}
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement