java多线程的基本概念和简单操作

来源:互联网 发布:数据录入外包 编辑:程序博客网 时间:2024/06/06 12:41

线程是一个程序内部的顺序控制流 简单说,线程是程序里面不同的执行路径

main方法就是一个主线程

 

线程与进程的区别

 1每个进程都有独立的代码和数据空间(进程上下文),进程间的切换开销较大

 2线程可以看成轻量级的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器,线程切换开销小

多进程:在操作系统中同时运行多个任务

多线程:在同一应用程序中有多个顺序流同时进行

 

线程的创建与启动(2种)

方法一

public class MyThread extends Thread{@Overridepublic void run() {// TODO Auto-generated method stub}}
public static void main(String[] args) {MyThread mt=new MyThread();     mt.start();}

方法二

public class MyThread implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stub}}
public static void main(String[] args) {MyThread mt=new MyThread();Thread thread=new Thread(mt);   thread.start();}

线程状态转换




线程控制的基本方法

isAlive() 判断线程是否还“活”着 即线程是否未终止

getPriority() 获取线程的优先级值

setPriority() 设置线程的优先级数值

Thread.sleep()指定当前线程睡眠时间

Thread.currentThread()获取当前线程

join() 调用某线程的此方法,将当前线程与此线程合并,即等待次线程结束再恢复运行当前线程

Thread.yield() 投降 让出cpu当前线程进入就绪状态等待cpu调度

wait() 当前线程进入对象的wait pool

notify()/notifyAll() 唤醒对象的wait pool中的一个/所有等待线程

 线程的结束

结束线程不要使用stop()方法

可以使用退出标志flag结束线程

public class MyThread extends Thread{private boolean flag=true;@Overridepublic void run() {// TODO Auto-generated method stubwhile(flag){//do something}}public void stopThread(){this.flag=false;}}

sleep方法

try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}

1sleep是静态方法,控制的是当前线程

2当前线程的睡眠是为了给其他线程提供机会

3 当线程苏醒后,为可运行状态,而不是就绪状态

join方法

在当前线程中调用a线程此方法,则表示当前线程与a线程合并,就是在a线程结束后再运行当前线程

//MyThread类中的方法public void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){System.out.println("this is mythread");}}//mainpublic static void main(String[] args) {MyThread mt=new MyThread();mt.start();try {mt.join();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}for(int i=0;i<10;i++){System.out.println("mainthread");}}

运行结果:

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

this is mythread

mainthread

mainthread

mainthread

mainthread

mainthread

mainthread

mainthread

mainthread

mainthread

mainthread



yield方法

此方法可以理解为让步,调用此方法的线程从运行状态变为就绪状态,即这个线程做出退让,让cpu先去执行那些运行状态的线程,但并不代表此线程就不会执行了

public void run() {// TODO Auto-generated method stubfor(int i=0;i<100;i++){if(i%10==0){Thread.yield();}System.out.println(Thread.currentThread().getName()+"-->"+i);}}
public static void main(String[] args) {MyThread mt1=new MyThread();MyThread mt2=new MyThread();Thread thread1=new Thread(mt1,"线程1");Thread thread2=new Thread(mt2,"线程2");thread1.start();thread2.start();}

线程2-->35

线程1-->10

线程2-->36

线程1-->11

线程2-->37

/////////////////////////////////////

线程1-->25

线程2-->50

线程2-->51

线程2-->52


第一种情况当线程110的倍数时做出让步于是cpu执行线程2

但第二种情况却没有

 

线程的优先级

Java线程的优先级是一个整数,其取值范围是1 (Thread.MIN_PRIORITY ) - 10 (Thread.MAX_PRIORITY)。

Thread源代码里对NORM_PRIORITY (数值为5) 的注释是“线程默认的优先级”

通过setPriority()设置线程优先级,优先级高的线程比优先级低的线程有更大的几率得到执行,即CPU分配更多的时间去执行,但并非绝对,高优先级的线程一定先于低优先级的线程执行




阅读全文
0 0
原创粉丝点击