您的位置:首页 > 编程语言 > Java开发

Java学习日记之分身有术:线程

2015-06-19 16:46 771 查看
程序运行中,有进程和线程两个概念。他们分别对应一个动态执行过程并相互联系,是程序运行中比较重要的执行过程。

程序是静态的代码,当程序执行一个程序的时候就是进程。进程从准备执行,等待资源,到最终释放退出资源,有一个独立的生存空间和完整的生命周期。

每一个任务就是一个进程,用于损失险一项具体功能。线程是进程的一部分,一个任务可以分为多个子过程,这就是线程。多线程可以提高进程的执行效率。线程也有自己的运行空间和生命周期。

使用线程可以是开发过程简单高效。当硬件系统为多处理核心的时候,使用多线程能发挥多出咯I的最大功效。

线程的生命周期是一个动态的过程,其生命周期包括5个状态:

·新建状态:使用new关键字创建了线程对象之后,线程处于新建状态,此时的线程还没有分配资源,不能执行。

·就绪状态:线程调用start()方法之后就会进入就绪状态,此时的线程拥有了部分资源,只等待Java虚拟机分配CPU时间片执行。

·运行状态:线程获得CPU时间片之后进入运行状态,开始执行。

·阻塞状态:线程运行的时候,当资源不足的时候就会处于阻塞状态,一旦资源条件具备,Java虚拟机就会再度分配时间片,继续执行线程体。

·死亡状态:线程完成工作或者强制终止的时候就处于死亡状态。

这5种状态可以相互转换。

1.线程的实现:线程的创建可以通过Thread类或Runnable接口实现。

示例:

public class Thread001 {

/**
* 线程的创建和实现
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadExample thread1,thread2,thread3;//生成三个子线程,依次执行
thread1=new ThreadExample("a");
thread2=new ThreadExample("b");
thread3=new ThreadExample("c");
thread1.start();       //执行后会调用run()方法
thread2.start();
thread3.start();
}

}
class ThreadExample extends Thread
//Thread类是java.lang包中的类,该类是系统默认加载的,所以不需要自己手动导入包
{
ThreadExample(String s)
{
setName(s);
}
public void run()
{
System.out.println("执行子线程"+getName());
System.out.println("执行子线程"+getName());
System.out.println("执行子线程"+getName());
}
}
示例2:

public class Thread002 {

/**
* 通过继承Runnable接口实现多线程
* Runnable接口也在java.lang包中。Runnable接口中只有一个方法run(),用于实现线程的执行。
* 继承该接口就要重写run()方法。
* Runnable接口不直接实现多线程功能,需要在Thread类的构造方法public Thread(Runnable target)中实现
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread thread1,thread2,thread3;
thread1=new Thread(new ThreadExample2("a"));//线程参数是为了实现Runnable接口的对象
thread2=new Thread(new ThreadExample2("b"));
thread3=new Thread(new ThreadExample2("c"));
thread1.start();
thread2.start();
thread3.start();
}
}
class ThreadExample2 implements Runnable     //实现Runnable接口的类
{
String name;
ThreadExample2(String s)
{
name=s;
}
public void run()//重写run()方法
{
System.out.println("执行子线程:"+name);
}
}
2.线程的调度与优先级

线程都是并发执行的,当有多个线程处于运行状态的时候,需要确定其优先级。

一般情况下,多个线程是同时运行的,优先级相同,程序按照线程的先后顺序依次执行。Java还提供了一种方式,我们可以给线程赋予不同的优先级。

Java线程的优先级有10个级别,个级别的代号为1~10,数字越大优先级越高,程序就最先被执行。

示例3:

public class Thread003 {

/**
* 线程的调度和优先级
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadExample3 thread1,thread2,thread3;
thread1=new ThreadExample3("a");
thread2=new ThreadExample3("b");
thread3=new ThreadExample3("c");

thread3.setPriority(Thread.MAX_PRIORITY);//设置子线程的优先级
thread2.setPriority(5);
thread1.setPriority(1);

thread1.start();
thread2.start();
thread3.start();
}

}
class ThreadExample3 extends Thread
{
ThreadExample3(String s)
{
setName(s);
}
public void run()
{

System.out.println("执行子线程:"+getName());
}
}
3.线程的互斥

多线程的设计中,为避免多个线程访问同一资源而出现冲突,我们在程序中设置了“互斥锁”的概念。

我们把共享的资源称为临界资源,通过一个信号装置通知各个线程,同一时间只能有一个线程访问该临界资源。这个信号装置就是互斥锁。

示例4:

public class Thread004 {

/**
* 线程互斥
* 互斥锁的关键字是synchronized,使用方法如下:
* synchronized(临界资源)
* {
* 	   //临街资源处理
* }
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account1=new Account(100);
ThreadExample4 thread1,thread2,thread3;
thread1=new ThreadExample4(account1);
thread2=new ThreadExample4(account1);
thread3=new ThreadExample4(account1);

thread1.start();
thread2.start();
thread3.start();
}

}
class ThreadExample4 extends Thread
{
Account ac;
ThreadExample4(Account ac)
{

this.ac=ac;
}
public void run()        //取钱
{
ac.get(100);
}
}
class Account  //账户类
{
double balance;
public Account(double d)
{
balance=d;
}
public synchronized void get(double i)    //取钱的方法
{
if(balance>0)
{
try{
Thread.sleep(1000);
}catch(Exception e){}
balance=balance-i;
System.out.println("取了"+i+",还剩"+balance);
}
else
{
System.out.println("余额不足,无法操作");
}
}
}
4.线程的同步

线程的同步问题典型是生产者-消费者模型。线程的同步实在线程的互斥的情况下,十多个不同的线程能够以此执行存取操作。线程同步使用wait()方法和notify()方法,这两个方法在object类中默认集成到所有的类中,可以直接调用。

示例5:

public
4000
class Thread005 {

/**
* 实现一个货架类,货架的最大容量是100,可以存货和取货。
* 另外实现连个线程类,分别执行存货操作和取货操作,使他不空的时候可以取货,满的时候不能存货。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Shelf shelf=new Shelf(100);              //货栈类
GetThread thread1=new GetThread(shelf);  //取货类
PutThread thread2=new PutThread(shelf);  //存货类

thread1.start();                         //取货
thread2.start();						 //存货
}

}
class GetThread extends Thread
{
Shelf sh;
GetThread(Shelf sh)
{
this.sh=sh;
}
public void run()              //取货5次,每次60件
{
for(int i=0;i<5;i++)
{
sh.get(60);
}
}
}
class PutThread extends Thread
{
Shelf sh;
PutThread(Shelf sh)
{
this.sh=sh;
}
public void run()         //存货5次,每次40件
{
for(int i=0;i<5;i++)
{
sh.put(40);
}
}
}
class Shelf                  //货架类
{
int number;
public Shelf(int i)
{
number=i;
}
public synchronized void get(int i)  //取货方法,带有互斥锁,不能同时两次取货
{
System.out.println("想要取货"+i+"件,货还有"+number+"件");
if(number-i<0)              //货不够,不能取
{
try{
System.out.println("货不够,暂时不能取货");
wait();               //等待被唤醒
}catch(Exception e){}
}
number=number-i;
System.out.println("可以取货,取了"+i+"件,还有"+number+"件");
notify();               //唤醒其他互斥锁
}

public synchronized void put(int i)             //存货方法
{
System.out.println("想要存货"+i+"件,货栈中还有"+number+"件");
if(number+i>100)               //表示货栈已满
{
try{
System.out.println("货栈已满,暂时不能存货");
wait();                //等待被唤醒
}catch(Exception e){}
}
number=number+i;
System.out.println("可以存货,存了"+i+"件,还有"+number+"件");
notify();  //唤醒其他互斥锁
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息