1. public class Test extends Object {
    2. public static void main(String[] args) throws Exception {
    3. /*
    4. 命名操作:
    5. 在线程启动前命名,尽量不重名,线程运行时不改名
    6. 1. public Thread(Runnable target, String name)
    7. 2. public final String getName()
    8. 3. public final synchronized void setName(String name)
    9. 4. public static native Thread currentThread()
    10. */
    11. Thread thread0 = new Thread(new MyThread(), "自己命名的线程0");
    12. Thread thread1 = new Thread(new MyThread());
    13. thread1.setName("自己命名的线程1");
    14. Thread thread2 = new Thread(new MyThread());
    15. Thread thread3 = new Thread(new MyThread());
    16. /*
    17. 线程优先级 1-10
    18. public final static int MIN_PRIORITY = 1;
    19. public final static int NORM_PRIORITY = 5;
    20. public final static int MAX_PRIORITY = 10;
    21. 优先级高,有可能先执行,不是一定
    22. */
    23. thread0.setPriority(10);
    24. thread1.setPriority(1);
    25. thread2.setPriority(1);
    26. thread3.setPriority(1);
    27. thread0.start();
    28. thread1.start();
    29. thread2.start();
    30. thread3.start();
    31. System.out.println(Thread.currentThread().getName());
    32. ;
    33. }
    34. }
    35. //线程操作类
    36. class MyThread implements Runnable {
    37. @Override
    38. public void run() {
    39. /*
    40. 线程的休眠操作
    41. public static native void sleep(long millis) throws InterruptedException;
    42. */
    43. for (int i = 0; i < 20; i++) {
    44. try {
    45. Thread.sleep(500);
    46. } catch (InterruptedException e) {
    47. e.printStackTrace();
    48. }
    49. System.out.println(Thread.currentThread().getName() + ",i=" + i);
    50. }
    51. }
    52. }