线程

来源:互联网 发布:买车下个什么app软件 编辑:程序博客网 时间:2024/05/21 09:04

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

线程是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成,同时线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一个线程可以创建和撤销另外一个线程,同一个进程中的多个线程之间可以并发的执行。由于线程之间每一个程序至少拥有一个线程,那就是程序本身。

通常一个进程中包含有多个线程,每个线程都是作为CPU的基本单位,是花费最小开销实体。同一个进程中的各个线程,都可以共享该进程所拥有的的资源,所有线程都具有相同的地址空间(进程的地址空间)这意味着线程可以访问该地址空间的每一个虚地址,此外,还可以访问进程所拥有的的已经打开的文件,定时器,信号量机构等。

线程的实现有两种方法:一种是继承Thread类,另外一种是实现Runnable接口

//模拟卖票程序:多个窗口卖票
import java.lang.*;

创建线程方法一;继承Thread
class MyThread extends Thread{
private static int ticket=20;
public void run(){
while(true){
if(ticket>0){

System.out.println("我是线程"+Thread.currentThread().getName()+"我在卖票"+ticket--);
}
}
}
}
class ThreadDemo3 extends Thread{

public static void main(String args[]){
MyThread mt1=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();
mt1.start();
mt2.start();
mt3.start();

}
}

//创建线程二:实现接口:Runnable

class MyRun implements Runnable {
private int ticket=20;
Object obj=new Object();
public void run() {
while(true){
synchronized(obj){
if(ticket>0){
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在卖票"+ticket--);
}
}

}

}
}
class ThreadDemo3 {
public static void main(String args[]){
MyRun mr=new MyRun();
Thread t1=new Thread(mr);
Thread t2=new Thread(mr);
Thread t3=new Thread(mr);
Thread t4=new Thread(mr);
Thread t5=new Thread(mr);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();

}
}
线程死锁:如果线程中有几个竞争资源的并发线程,那么保证均衡是很重要的。均衡是指每个线程在执行过程中都能充分访问有限的资源。系统中没有饿死和死锁的线程。预防死锁的方法有两种:一是:同步代码块,二是同步函数,无论哪种实现方法,都是要用到锁(synchronzied)

死锁的案例:
class DeathDemo{
public static void main(String args[]){
Demo d=new Demo(true);
Thread t1=new Thread(d);
Thread t2=new Thread(d);
t1.start();
t2.start();
}

}
class Demo implements Runnable{
  private boolean flag=true;
Demo(boolean flag){
this.flag=flag;
}
public void run(){
if(flag){
synchronized(Lock.locka){
System.out.println("1");
synchronized(Lock.lockb){
System.out.println("2");
}
}
}
else{
synchronized(Lock.lockb){
System.out.println("3");
synchronized(Lock.locka ){
System.out.println("4");
}
}
}

}
}
class Lock {
static Object locka=new Object();
static Object lockb=new Object();
}

//同步代码块和同步锁
public class ThreadDemo5{
public static void main(String args[]){

tickets t=new tickets();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
//Thread t3=new Thread(t);
//Thread t4=new Thread(t);
t1.start();
try{Thread.sleep(100);}catch(Exception e){System.out.println("错误");}
t.flag=false;
t2.start();

//t3.start();
//t4.start();


}
}
class tickets implements Runnable{
private int tickets=1000;
Object obj=new Object();
boolean flag=true;
public void run(){
if(flag){
while(true){
synchronized(obj){
show();

}
}
}
else{
while(true){
show();
}
}

}
public synchronized void show(){
synchronized(obj){
if(tickets>0){
try{Thread.sleep(100);}catch(Exception e){System.out.println("错误");}
System.out.println(Thread.currentThread().getName()+"---show---"+tickets--);
}
}
}

}












原创粉丝点击