多线程笔记

来源:互联网 发布:服装店记账软件手机 编辑:程序博客网 时间:2024/06/05 14:17

创建线程

1、创建一个Thread类,或一个Thread子类对象。

2、创建一个实现Runnable接口的对象。

new MyThread().start()

new Thread(new MyRunnable()).start()


Thread类常用方法

public void run(); 线程相关代码现在该方法中,一般需要重写

public void start(); 启动线程的方法

public static void sleep(long m); 线程休眠m毫秒的方法

public void join(); 优先执行调用join()方法的线程


Runnable接口

只有一个方法run();

Runnable实java中以实现线程的接口

任何实现线程功能的类都必须实现该接口


线程优先级

1-10级  main方法默认5级

public int getPriority(); 获取线程优先级的方法

public void setPriority(int newPriority) 设置线程优先级的方法


synchronized关键字 同步


wait()方法:中断方法的执行,使线程等待

notify()方法:唤醒处于等待的某一个线程,使其结束等待