First, what is the application
Software can perform, such as QQ, micro-letters, etc.
Second, what is the process
Application process is running, it is a collection of multiple threads . Any software up and running there will certainly be a long process on display, the implementation of the program
Third, what is the thread
The process is being run independently perform a path, in the process, there must be a thread, the thread is the main thread
Fourth, what multithreading
It is to improve the efficiency of the program , not only to improve the broadband speed and efficiency program
Fifth, the multi-threaded application scenarios
Multi-threaded download, QQ, reptiles, front-end development ajax (asynchronous upload), distribution job (need to perform more than one task scheduling), the efficient use of multi-threaded programs reflect
6, how to create multi-threaded
1. Use the class inheritance Therad way
class CreateThread the extends the Thread { // write multithreaded code needs to do is run method publicvoid run() { for (you = 0; i <10; i ++ ) { System.out.println("i:" + i); } } } publicclass ThreadDemo { publicstaticvoid main(String[] args) { System.out.println("-----多线程创建启动-----"); new CreateThread().start(); //执行 } }
2.使用实现runlabe接口方式
class CreateRunnable implements Runnable { @Override publicvoid run() { for (inti = 0; i< 10; i++) { System.out.println("i:" + i); } } } publicclass ThreadDemo2 { publicstaticvoid main(String[] args) { System.out.println("-----多线程创建开始-----"); // 1.创建一个线程 CreateRunnable createThread = new CreateRunnable(); // 2.开始执行线程 注意 开启线程不是调用run方法,而是start方法 System.out.println("-----多线程创建启动-----"); Thread thread = new Thread(createThread); thread.start(); System.out.println("-----多线程创建结束-----"); } }
3.使用匿名内部类方式
System.out.println("-----多线程创建开始-----"); Thread thread = new Thread(new Runnable() { public void run() { for (int i = 0; i< 10; i++) { System.out.println("i:" + i); } } } thread.start(); //执行 System.out.println("-----多线程创建结束-----");
4.callable
5.使用线程池创建线程
七、使用继承Thread类还是使用实现Runnable接口好?
使用runnable接口好,原因实现了接口还可以继续继承,继承了类不能再继承。
八、什么是同步
必须按照顺序来执行,代码从上往下进行执行
九、什么是异步
多线程,多线程之间每个线程互不影响。
十、获取线程对象以及名称
1.常用线程api方法
启动线程:start()
获取当前线程对象:currentThread()
获取当前线程ID:getID()
获取当前线程名称:getName()
休眠线程:sleep(long mill)
停止线程:Stop()
2.常用线程构造函数
分配一个新的 Thread 对象:Thread()
分配一个新的 Thread 对象具有指定的 name正如其名:Thread(String name)
分配一个新的 Thread 对象:Thread(Runabler)
分配一个新的 Thread 对象:Thread(Runabler,String name)
十一、多线程运行状态
1、新建状态
2、就绪状态
3、运行状态
4、阻塞状态
5、死亡状态