一、概念
1、进程:程序是计算机指令的集合,它以文件形式存储在磁盘上,而进程就是一个执行中的程序,而每一个进程都有其独立的内存空间和系统资源。
2、线程:是运行程序(进程)中单个顺序的小程序,一个进程可以由多个线程组成,而这多个线程共享同一个存储空间,这使得线程间的通信比较容易。在一个多进程的程序中,如果要切换到另一个进程,需要改变地址空间的位置。然而在多线程的程序中,就不会出现这种情况,因为他们位于同一个内存空间内,只需改变运行的顺序即可。
二、线程的创建:
1、通过Runnable接口的方式创建:
Runnable接口的定义
- <span style="font-size:18px;">public interface Runnable{
- public abstract void run();
- }</span>
public interface Runnable{
public abstract void run();
}
- <span style="font-size:18px;">//线程创建:runnable 对象 是指实现了Runnable接口类的对象
- Thread t=new Thread(runnable 对象);
- //线程启动
- t.start();</span>
//线程创建:runnable 对象 是指实现了Runnable接口类的对象
Thread t=new Thread(runnable 对象);
//线程启动
t.start();
2、通过继承Thread类来创建线程:
Thread类本身也实现了Runnable接口,所以只要让一个类继承Thread类,并重写 run() 方法,也就创建了线程
三、线程的优先级
线程的执行是一种抢占方式,优先级高的比优先级低的要获得更多的执行时间,如果想让一个线程比其他线程有更多的时间运行,可以通过设置线程的优先级解决。
可以通过调用“setPriority()”方法来设置:
- <span style="font-size:18px;">public final void setPriority(int newpriority);</span>
public final void setPriority(int newpriority);
newpriority是一个1~10之间的正整数,数值越大,优先级越高,其中,5表示默认优先级
- <span style="font-size:18px;">public class Test {
- public static void main(String[] args) {
- Compute2 t2=new Compute2();
- Compute3 t3=new Compute3();
- t2.setPriority(10);
- t2.start();
- t3.start();
- }
- }</span>
public class Test {
public static void main(String[] args) {
Compute2 t2=new Compute2();
Compute3 t3=new Compute3();
t2.setPriority(10);
t2.start();
t3.start();
}
}
四、线程休眠
线程的休眠,是指线程暂时处于等待的一种状态,就是线程暂时停止运行了,需要调用Thread类的sleep()方法,可以让线程在指定时间内处于暂时停止状态,时间结束,继续执行未完成的任务。方法原型:
- <span style="font-size:18px;">//mills指线程休眠的毫秒数
- public static native void sleep(long mills)throws interruptedException</span>
//mills指线程休眠的毫秒数
public static native void sleep(long mills)throws interruptedException
- <span style="font-size:18px;">public class compute extends Thread {
- public void run() {
- for (int i = 0; i < 10; i++) {
- System.out.println(i);
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }</span>
public class compute extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
五、线程唤醒
线程唤醒是指,使线程从休眠等待状态进入可执行状态,可以调用interrupt()方法。例:
- <span style="font-size:18px;">public class Thread1 {
- public static void main(String[] args) {
- compute t=new compute();
- t.start();
- t.interrupt();
- }
- }
- class compute extends Thread{
- public void run(){
- System.out.println("在工作中,不要打扰");
- try {
- sleep(10000);
- } catch (InterruptedException e) {}
- System.out.println("哦,电话来了");
- }
- }</span>
public class Thread1 {
public static void main(String[] args) {
compute t=new compute();
t.start();
t.interrupt();
}
}
class compute extends Thread{
public void run(){
System.out.println("在工作中,不要打扰");
try {
sleep(10000);
} catch (InterruptedException e) {}
System.out.println("哦,电话来了");
}
}
六、线程让步
所谓线程让步,就是使当前正在运行的线程对象退出运行状态,让其他线程运行,可以调用yield()来实现,此方法不能将运行权让给指定线程,只是允许这个线程把运行权让出来,至于给谁,这就是抢占功能的事情了。
- <span style="font-size:18px;">public class Compute1 extends Thread {
- public void run() {
- for (int i = 0; i < 10; i++) {
- System.out.println("这个数字是:" + i);
- yield();
- }
- }
- }</span>
public class Compute1 extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("这个数字是:" + i);
yield();
}
}
}
七、线程同步
1、同步块
同步块是使具有某个对象监视点的线程,获得运行权限的一种方法,每个对象只能在拥有这个监视点的情况下,才能获得运行权限。结构如下:
- <span style="font-size:18px;">synchronized(someobject){
- 代码段
- }</span>
synchronized(someobject){
代码段
}
someobject 是一个监视点对象,可以是实际存在的,也可以是假设的。在很多程序中,这个监视点对象都是假设的。就像是一把锁,给线程上了锁,其他线程就会被拒之门外,直到这个线程执行完毕,才会将这个锁交给其他线程。
- <span style="font-size:18px;">public class Compute1 extends Thread {
- static Object obj=new Object();
- public void run() {
- synchronized(obj){
- for (int i = 0; i < 10; i++) {
- System.out.println("这个数字是:" + i);
- }
- }
- }
- }</span>
public class Compute1 extends Thread {
static Object obj=new Object();
public void run() {
synchronized(obj){
for (int i = 0; i < 10; i++) {
System.out.println("这个数字是:" + i);
}
}
}
}
2、同步方法
结构如下:
- <span style="font-size:18px;">synchronized void f(){
- // 代码
- }</span>
synchronized void f(){
// 代码
}
- <span style="font-size:18px;">public class Compute1 extends Thread {
- int i = 10;
- static Object obj = new Object();
- synchronized void print() {
- System.out.println(Thread.currentThread());
- i--;
- }
- public void run() {
- while (i > 0) {
- print();
- try {
- sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }</span>
public class Compute1 extends Thread {
int i = 10;
static Object obj = new Object();
synchronized void print() {
System.out.println(Thread.currentThread());
i--;
}
public void run() {
while (i > 0) {
print();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}