52ky 发表于 2022-9-23 10:40:36

[零基础学JAVA]Java SE应用部分-24.多线程(02)


上季内容回忆:
多线程的两种终结方法:
·Thread
·Runnable
终究都请求使用Thread类中start()方法启动多线程
本季首要知识点:
线程的控制方法
线程的状态转换
一切的线程实际上是一起启动的,只是抢占CPU的资源的顺序不一样。
线程称号的设置及获得
在线程中一切的操作类都是在一个类——Thread类.
从提供的方法上来看:多线程中提供了getName和setName则应当可以揣度出在Thread类中必定有一个name属性。
ThreadDemo01:
运行成果:
在程序中,我们没有设置Thread-0称号哈,下面我们设置其称号
再来验证下作用
main线程是由目标进行调用的,所以是由主线程控制
从以上标题我们应当认识,我们之前所运行的一切的JAVA程序都是在线程上运行的。
java类称号--gt;启动JVM的进程
里边的main方法实际上就是一个在JVM进程上划分的线程。
假如没有为线程设置名称,则线程的默许名称为Thread-x(x:表明数字,从0开始)
假如计数是往上增1,则一切目标肯定是共享同一个变量,则此变量肯定是static类型的。
看下作用:
问题:
问在JAVA程序中,每一次运行最少启动几个线程呢?
·主线程(main)
·垃圾搜集线程(GC)
方才设置名称的时候是在Thread类的目标创建完以后在设置名称的,可以在结构方法处设置线程称号:publicThread(Runnabletarget,Stringname);
成果出来了
线程控制基本方法
方法称号以isXxx最初的,一般都是回来boolean类型。
激活线程
线程是通过start()方法启动的,在线程启动之前肯定是死的,可是线程启动以后再判别呢?
此程序更能验证一点——多线程的程序的输出是不固定的,谁先谁后都应当充沛考虑到。
设置后台线程
Linux下可以很清理的看见有后台线程。
假如不设置后台线程会如何,看一下正常启动线程
程序进入死循环
如今设置程序后台运行
我们发现程序运行一段时间后自己跑后台执行了,程序在前台看不见了
线程的休眠
假如说如今不使用Thread类中提供好的sleep方法,那么如何可以让线程暂时停止运行呢?
发现程序运行有点快,我们加个for空循环
如今可以清理看见程序在一个个运行了
多线程类中提供专门的休眠方法——sleep
publicstaticvoidsleep(longmillis)--gt;休眠毫秒
throwsInterruptedException--gt;抛出了中止异常
程序达到了一样的作用哈~三个线程同一时间跳出来哈~这就是一个多线程休眠的操作
线程的强行运行
publicfinalvoidjoin()--gt;证实此方法不能被子类覆写
throwsInterruptedException
我们看一下这个t.txt文件哈~当i值为100时,强行执行Thread-0线程
总结
1、获得当时线程:
publicstaticThreadcurrentThread();
2、获得线程的名称:
publicStringgetName();
3、设置线程的名称:
publicvoidsetName(Stringname)
在结构方法上也有支持:
-publicThread(Stringname);直接在结构方法处设置线程的名称
-publicThread(Runnabletarget,Stringname)
4、JAVA程序启动时最少启动两个线程
·main:主方法
·gc:垃圾搜集
5、线程的休眠
假如不用Thread类中提供的方法,则可以通过空循环的方法终结推迟操作
publicstaticvoidsleep(毫秒)throwsInterruptedException
6、线程的强行运行
publicfinalvoidjoin()throwsInterrtupedException
#############################################

(Memories from last season:
Two termination methods for multithreading:
·Thread
·Runnable
After all, it is requested to use the start() method in the Thread class to start multi-threading
Top knowledge points this season:
thread control method
state transitions of threads
All threads are actually started together, but the order of preempting CPU resources is different.
Thread title setting and acquisition
All operations in a thread are in one class - the Thread class.
Judging from the provided methods: if getName and setName are provided in multi-threading, it should be inferred that there must be a name attribute in the Thread class.
ThreadDemo01:
Operational results:
In the program, we did not set the Thread-0 title, let's set its title below
Verify the effect again
The main thread is called by the target, so it is controlled by the main thread
From the above title we should realize that all the JAVA programs we have run before are running on threads.
java class name --gt; the process that started the JVM
The main method inside is actually a thread divided on the JVM process.
If no name is set for the thread, the default name of the thread is Thread-x (x: indicates a number, starting from 0)
If the count is increased by 1, then all objects must share the same variable, then this variable must be of static type.
Take a look at the effect:
question:
In a JAVA program, what is the minimum number of threads to start each run?
The main thread (main)
Garbage collection thread (GC)
When the name was just set, the name was set after the target of the Thread class was created, and the thread name can be set at the structure method: publicThread(Runnabletarget, Stringname);
The result is out
Basic method of thread control
The method name starts with isXxx and usually returns a boolean type.
activate thread
The thread is started by the start() method. It must be dead before the thread is started, but how can it be judged after the thread is started?
This program can better verify one point - the output of multi-threaded programs is not fixed, and whoever comes first should be fully considered.
set background thread
Under Linux, you can see that there are background threads very cleanly.
What if you don't set a background thread, look at the normal startup thread
The program enters an infinite loop
The program is now set to run in the background
We found that the program ran in the background after running for a period of time, and the program was invisible in the foreground
thread sleep
If the good sleep method provided in the Thread class is not used now, how can the thread temporarily stop running?
Found that the program runs a little faster, we add a for empty loop
Now you can clean up and see that the programs are running one by one
A special sleep method is provided in the multi-threaded class - sleep
publicstaticvoidsleep(longmillis)--gt; sleep milliseconds
throwsInterruptedException--gt; throws an aborted exception
The program achieves the same effect~Three threads jump out at the same time~This is a multi-threaded sleep operation
Forced execution of threads
publicfinalvoidjoin()--gt; confirms that this method cannot be overridden by subclasses
throwsInterruptedException
Let's take a look at this t.txt file~ When the i value is 100, the Thread-0 thread is forcibly executed
Summarize
1. Get the current thread:
publicstaticThreadcurrentThread();
2. Get the name of the thread:
publicStringgetName();
3. Set the name of the thread:
publicvoidsetName(Stringname)
There is also support for structural methods:
-publicThread(Stringname); set the name of the thread directly at the structure method
-publicThread(Runnabletarget, Stringname)
4. At least two threads are started when the JAVA program starts
main: main method
gc: garbage collection
5. Thread sleep
If you do not use the methods provided in the Thread class, you can terminate the deferred operation through the method of an empty loop
publicstaticvoidsleep(ms) throwsInterruptedException
6, the forced operation of the thread
publicfinalvoidjoin() throwsInterrtupedException
#############################################)




页: [1]
查看完整版本: [零基础学JAVA]Java SE应用部分-24.多线程(02)