[零基础学JAVA]Java SE应用部分-23.多线程(01)
版权声明:原创作品,谢绝转发!否则将追查法律责任。本季方案
多线程这一个部分,重点掌握以下几点(概念):
·线程与进程的区别
·java中多线程的实现方法及区别
·java线程的同步与死锁
多线程的基本概念(1)
DOS开始的:假如一旦呈现了病毒,则操作系统就会死掉,由于DOS在同一个时刻段上只能有一个程序运行。
Windows:假如呈现了病毒操作系统是不是仍然可以使用呢?
可是电脑的CPU有几个?一个?多进程是将CPU的资源进行划分,在同一个时刻段上会有多个程序运行,可是在同一个时刻点上只能有一个程序运行。
进程与线程的连接?
操作系统可以有多个进程
每个进程上会划分出多个线程
假如进程不见了,则线程也会不见
假如线程不见了,则进程未必会不见。
多线程的基本概念(2)
多线程本身仍是需求操作系统支持的。也就是说使用多线程会提供资源的利用率。
JAVA线程的实现
程序顺次履行线程A、线程B、线程C,如同发挥不出同一个时刻段上有多个程序运行哈~
以上代码是通过run方法调用的,没有启动多线程机制。实际上注意一点:各个操作系统都有各自的资源划分,也就是说,假如要想在一个操作系统上实现多线程程序,则必定不能直接去调用run(类中的方法)启动,而应当通知操作系统,由操作系统进行分配。
假如想要启动多线程,则必定调用start()方法,start方法可以调用被子类覆写过的run方法
我们如今看下程序能否启动多线程
通过start()方法的确调用run方法,此刻的程序的确是一起运行着,并且是替换运行,谁抢到了CPU资源,谁就运行。
为何非要调用start()方法启动呢?
JDK中Thread类的定义
其间started是在方法中定义一个boolean变量,判断这个线程是不是己被启动,假如没有启动,则履行此段代码,表明线程现已启动,线程参加安排妥当状况,调用start0这个私有方法,此方法表明要调用操作系统的函数。
native是一个关键词
在JAVA中有种技术:JNI(JavaNativeInterface),可以使用JAVA程序调用本机操作系统的函数,以到达某些功能。
假如再次启动一个现已启动的线程,则会如何呢?
程序会抛出一个IllegalThreadStateException()异常
记住:只要是线程启动一次就OK啦。
以上程序实际上也存在问题?
假如一个类有必要承继Thread类才干实现多线程,则此类必定就不能再承继别的的类了,即:假如使用Thread会受到单承继的限制,所以通常实现多线程都是实现Runnable接口。
在Thread类的结构方法处:
publicThread(Runnabletarget)--gt;表明只要是Runnable接口的子类目标都可以向里面传递。
验证下作用,多线程的确起作用了哈~
定论:不管那种方法实现多线程,终究永远都是通过Thread类启动多线程。
Thread类与Runnable接口(1)
Thread类也是实现了Runnable接口哈~
Thread类与Runnable接口(2)
1、预防单承继限制
2、可以到达多个线程的资源共享
验证下作用哈~
我们发现这个程序有点问题,三个线程各占了10张票,总共卖了30张票,为何会这样呢?由于privateintticket=10;这个属性,一旦实现了下面的代码:
MyThreadmt1=newMyThread();
MyThreadmt2=newMyThread();
MyThreadmt3=newMyThread();
履行之后就实现实例化操作,mt1、mt2、mt3各自占各自的属性,即各占有10张票,这样就没有到达资源共享。
假如我们要实现资源共享,可以修改以上程序,看下面哈~
如今三个目标共卖10张票,到达了资源共享的作用哈~~~
假如使用了Runnable接口实现了多线程,则一切的线程目标一起占有同一个Runnable接口的子类目标,则证明多个线程共享资源。
定论:必定一个类实现Runnable接口实现多线程是最好的。
总结
1、进程与线程的区别
·线程是在进程基础上的划分
·进程是在操作系统上的划分
2、使用多线程可以发如今同一个时刻段上一切的程序是替换运行的。
3、多线程的实现有两种方法:
·实现Runnable接口
·承继Thread类
4、连接与区别:
·Thread类也是Runnable接口的子类
·假如要想启动线程,则有必要把自定义的实现了Runnable接口的子类目标放入Thread类中
·Runnable接口可以实现多个资源的共享
·绝对要优先使用Runnable接口,预防单承继的限制。
#################################################
(Copyright statement: original works, please do not forward! Otherwise, legal responsibility will be pursued.
Program this season
In this part of multithreading, focus on mastering the following points (concepts):
The difference between thread and process
The implementation method and difference of multi-threading in java
· Java thread synchronization and deadlock
The basic concept of multithreading (1)
DOS started: If a virus is present, the operating system will die, because DOS can only have one program running at the same time.
Windows: Is the operating system still usable if a virus is present?
But how many CPUs does the computer have? One? Multi-process is to divide the resources of the CPU. There will be multiple programs running at the same time period, but only one program can run at the same time point.
Process and thread connection?
An operating system can have multiple processes
Each process is divided into multiple threads
If the process disappears, the thread disappears
If the thread disappears, the process may not disappear.
Basic concepts of multithreading (2)
Multithreading itself still requires operating system support. That is to say, using multithreading will provide resource utilization.
The realization of JAVA thread
The program executes thread A, thread B, and thread C in sequence, as if there are multiple programs running at the same time period.
The above code is called through the run method, and the multi-threading mechanism is not started. In fact, pay attention to one point: each operating system has its own resource division, that is to say, if you want to implement a multi-threaded program on an operating system, you must not directly call run (the method in the class) to start, but should notify The operating system is assigned by the operating system.
If you want to start multiple threads, you must call the start() method, and the start method can call the run method overridden by the subclass
Let's now see if the program can start multithreading
The run method is indeed called through the start() method, and the programs at the moment are indeed running together, and they are running alternately. Whoever grabs the CPU resources will run.
Why do you have to call the start() method to start it?
Definition of Thread class in JDK
Among them, started is to define a boolean variable in the method to determine whether the thread has been started. If it is not started, this code is executed, indicating that the thread has been started, the thread participation is properly arranged, and the private method start0 is called. This method Indicates that a function of the operating system is to be called.
native is a keyword
There is a technology in JAVA: JNI (JavaNativeInterface), which can use JAVA program to call the functions of the native operating system to achieve certain functions.
What if an already started thread is started again?
The program will throw an IllegalThreadStateException() exception
Remember: as long as the thread is started once, it is OK.
Is there actually a problem with the above procedure?
If it is necessary for a class to inherit the Thread class to realize multi-threading, this class must not inherit other classes, that is, if Thread is used, it will be limited by single inheritance, so the implementation of multi-threading usually implements the Runnable interface.
At the structure method of the Thread class:
publicThread(Runnabletarget)--gt; indicates that as long as it is a subclass target of the Runnable interface, it can be passed to it.
Verify the effect, multi-threading does work~
Conclusion: No matter which method implements multithreading, it will always start multithreading through the Thread class.
Thread class and Runnable interface (1)
The Thread class also implements the Runnable interface~
Thread class and Runnable interface (2)
1. Preventing single inheritance restrictions
2. Resource sharing that can reach multiple threads
Verify it works~
We found that there is a problem with this program. The three threads each accounted for 10 tickets, and a total of 30 tickets were sold. Why is this? Because of the privateintticket=10; attribute, once the following code is implemented:
MyThreadmt1=newMyThread();
MyThreadmt2=newMyThread();
MyThreadmt3=newMyThread();
After the implementation, the instantiation operation is realized, and mt1, mt2, and mt3 each occupy their own attributes, that is, each occupies 10 tickets, so that resource sharing is not achieved.
If we want to achieve resource sharing, we can modify the above program, see below~
Now the three targets have sold 10 tickets in total, which has reached the role of resource sharing ha~~~
If the Runnable interface is used to implement multithreading, then all thread objects occupy the same subclass object of the Runnable interface, which proves that multiple threads share resources.
Conclusion: It is best for a class to implement the Runnable interface to implement multithreading.
Summarize
1. The difference between process and thread
Threads are divided on a process basis
Processes are divided on the operating system
2. Using multi-threading, it can be found that all programs on the same time period are running alternately.
3. There are two ways to implement multi-threading:
Implement the Runnable interface
·Inherit the Thread class
4. Connection and difference:
The Thread class is also a subclass of the Runnable interface
If you want to start a thread, it is necessary to put a custom subclass target that implements the Runnable interface into the Thread class
The Runnable interface can realize the sharing of multiple resources
·It is absolutely necessary to use the Runnable interface first to prevent the limitation of single inheritance.
#################################################)
页:
[1]