android线程使用

一、概念

1、进程:程序是计算机指令的集合,它以文件形式存储在磁盘上,而进程就是一个执行中的程序,而每一个进程都有其独立的内存空间和系统资源。

2、线程:是运行程序(进程)中单个顺序的小程序,一个进程可以由多个线程组成,而这多个线程共享同一个存储空间,这使得线程间的通信比较容易。在一个多进程的程序中,如果要切换到另一个进程,需要改变地址空间的位置。然而在多线程的程序中,就不会出现这种情况,因为他们位于同一个内存空间内,只需改变运行的顺序即可。

二、线程的创建:

1、通过Runnable接口的方式创建:

Runnable接口的定义

  1. <span style="font-size:18px;">public interface Runnable{  
  2.     public abstract void run();  
  3. }</span>  
public interface Runnable{
	public abstract void run();
}
  1. <span style="font-size:18px;">//线程创建:runnable 对象 是指实现了Runnable接口类的对象  
  2.     Thread t=new Thread(runnable 对象);  
  3.     //线程启动  
  4.     t.start();</span>  
//线程创建:runnable 对象 是指实现了Runnable接口类的对象
	Thread t=new Thread(runnable 对象);
	//线程启动
	t.start();

2、通过继承Thread类来创建线程:
Thread类本身也实现了Runnable接口,所以只要让一个类继承Thread类,并重写  run() 方法,也就创建了线程

三、线程的优先级

线程的执行是一种抢占方式,优先级高的比优先级低的要获得更多的执行时间,如果想让一个线程比其他线程有更多的时间运行,可以通过设置线程的优先级解决。

可以通过调用“setPriority()”方法来设置:

  1. <span style="font-size:18px;">public final void setPriority(int newpriority);</span>  
public final void setPriority(int newpriority);

newpriority是一个1~10之间的正整数,数值越大,优先级越高,其中,5表示默认优先级

  1. <span style="font-size:18px;">public class Test {  
  2.     public static void main(String[] args) {  
  3.     Compute2 t2=new Compute2();  
  4.     Compute3 t3=new Compute3();  
  5.     t2.setPriority(10);  
  6.     t2.start();  
  7.     t3.start();  
  8.     }  
  9. }</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()方法,可以让线程在指定时间内处于暂时停止状态,时间结束,继续执行未完成的任务。方法原型:

  1. <span style="font-size:18px;">//mills指线程休眠的毫秒数  
  2.     public static native void sleep(long mills)throws interruptedException</span>  
//mills指线程休眠的毫秒数
	public static native void sleep(long mills)throws interruptedException
  1. <span style="font-size:18px;">public class compute extends Thread {  
  2.     public void run() {  
  3.         for (int i = 0; i < 10; i++) {  
  4.             System.out.println(i);  
  5.             try {  
  6.                 sleep(1000);  
  7.             } catch (InterruptedException e) {  
  8.                 e.printStackTrace();  
  9.             }  
  10.         }  
  11.     }  
  12. }</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()方法。例:

  1. <span style="font-size:18px;">public class Thread1 {  
  2.     public static void main(String[] args) {  
  3.         compute t=new compute();  
  4.         t.start();  
  5.         t.interrupt();  
  6.     }  
  7. }  
  8. class compute extends Thread{  
  9.     public void run(){  
  10.         System.out.println("在工作中,不要打扰");  
  11.     try {  
  12.         sleep(10000);  
  13.     } catch (InterruptedException e) {}  
  14.     System.out.println("哦,电话来了");  
  15. }  
  16. }</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()来实现,此方法不能将运行权让给指定线程,只是允许这个线程把运行权让出来,至于给谁,这就是抢占功能的事情了。

  1. <span style="font-size:18px;">public class Compute1 extends Thread {  
  2.     public void run() {  
  3.         for (int i = 0; i < 10; i++) {  
  4.             System.out.println("这个数字是:" + i);  
  5.             yield();  
  6.         }  
  7.     }  
  8. }</span>  
public class Compute1 extends Thread {
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("这个数字是:" + i);
			yield();
		}
	}
}


七、线程同步

1、同步块

同步块是使具有某个对象监视点的线程,获得运行权限的一种方法,每个对象只能在拥有这个监视点的情况下,才能获得运行权限。结构如下:

  1. <span style="font-size:18px;">synchronized(someobject){  
  2.                 代码段  
  3.             }</span>  
synchronized(someobject){
				代码段
			}


someobject 是一个监视点对象,可以是实际存在的,也可以是假设的。在很多程序中,这个监视点对象都是假设的。就像是一把锁,给线程上了锁,其他线程就会被拒之门外,直到这个线程执行完毕,才会将这个锁交给其他线程。

  1. <span style="font-size:18px;">public class Compute1 extends Thread {  
  2.     static Object obj=new Object();  
  3.     public void run() {  
  4.         synchronized(obj){  
  5.             for (int i = 0; i < 10; i++) {  
  6.                 System.out.println("这个数字是:" + i);  
  7.                   
  8.             }  
  9.         }  
  10.           
  11.     }  
  12. }</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、同步方法

结构如下:

  1. <span style="font-size:18px;">synchronized void f(){  
  2. //          代码  
  3.         }</span>  
synchronized void f(){
//			代码
		}


 

  1. <span style="font-size:18px;">public class Compute1 extends Thread {  
  2.  int i = 10;  
  3.  static Object obj = new Object();  
  4.  synchronized void print() {  
  5.   System.out.println(Thread.currentThread());  
  6.   i--;  
  7.  }  
  8.  public void run() {  
  9.   while (i > 0) {  
  10.    print();  
  11.    try {  
  12.     sleep(1000);  
  13.    } catch (InterruptedException e) {  
  14.     e.printStackTrace();  
  15.    }  
  16.   }  
  17.  }  
  18. }</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();
   }
  }
 }
}


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值