java线程

一、程序、进程、线程
1、程序:一段指令的集合
2、进程:正在运行的程序
3、线程:进程中单一执行的流
二、区别与联系
1、进程与程序:程序是静态的,进程是动态的;一个程序对应多个进程,一个进程对应一个程序
2、进程与线程:进程有自己的独立空间,线程只能共享进程中的空间,进程中至少有一个线程
三、java多线程
1、多线程最大的好处是提高cpu利用率
2、可以执行多个任务,当程序等待资源时,而此时又不愿意为等待造成程序暂停,那么可以创建另外的线程进行其他的工作
3、java支持多线程
4、实现多线程

package Test;


public class ThreadTest extends Thread{
	private int count = 5;
	private String name;
	public ThreadTest(String name){
		this.name = name;
	}
	
	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println(name+count--);
		}
	}
}


package Test;


public class infunction {
	public static void main(String[] args) {
		ThreadTest tt1 = new ThreadTest("a");
		ThreadTest tt2 = new ThreadTest("b");
		tt1.start();
		tt2.start();
	}
}

如上述,两个线程同时启动,先抢占cpu资源的先执行,输出结果是
a5
b5
b4
b3
b2
b1
a4
a3
a2
a1
所以,Java使用线程可以多个任务同时进行
三、同步线程
1、线程通信分类:
“监视线程”通讯模型:一个线程控制多个线程运行程序
生产/消费模型:notify/wait来操作,例如生活中需求分析,当消费者消费数目达到一定量时,就会通知生产商生产,也就是程序中的notify,当生产到一定数目是,在程序中就会
等待——wait;
2、线程之间同时运行时,可能操作的是同一个对象时,可以同过synchronized来锁定线程运行,只允许一个线程运行该程序,synchronized只能有一个线程在执行,任何对象都有标识位,有0,1两种状态,synchronized执行代码块时会检查对象标识位,如果是1,执行程序,同时也将对象标识位设置为0,一直处于阻塞状态,一直等到标识位为1再执行下面的程序;
package Test;


public class Testinfunction {
	public static void main(String[] args) {
		Acount at = new Acount(5000);
		operationThread ot1 = new operationThread(at, "ATM", 3000);
		operationThread ot2 = new operationThread(at, "柜台", 3000);
		ot1.start();
		ot2.start();
	}
}


package Test;


public class operationThread extends Thread{
	private Acount acount;
	private int cash;
	private String operation;
	public operationThread(Acount acount,String operation,int cash){
		this.acount = acount;
		this.cash = cash;
		this.operation = operation;
	}
	public void run() {
		int count = acount.getcash(cash);
		if(count==-1){
			System.out.println("本人在"+operation+"取现失败,取现额度为"+count);
		}else{
			System.out.println("本人在"+operation+"取现成功,取现额度为"+count);
		}
	}
}


package Test;


public class Acount {
	private int count;
	public Acount(int count){
		this.count = count;
	}
	public int getcash(int cash){
		synchronized (this) {
			if(count<cash){
				return -1;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			count = count - cash;
			return count;
		}
	}
}

输出结果是:本人在ATM取现成功,取现额度为2000本人在柜台取现失败,取现额度为-1,,此时,如果没有synchronized的限制,输出的结果将是:本人在ATM取现成功,取现额度为2000本人在柜台取现失败,取现额度为-1000,这种结果产生的原因是两条线程同时运行,互不干扰,当第一条线程处于休眠时,第二条线程进入if循环,这就是多个线程操作一个对象时所产生的问题,所以要同过synchronized来解决;

package pass;

import java.util.ArrayList;

public class infunction {
	public static void main(String[] args) {
		ArrayList<dumpling> list = new ArrayList<dumpling>();
		Father fa = new Father(list);
		Mother mo = new Mother(list);
		mo.start();
		fa.start();
	}
}
package pass;

import java.util.ArrayList;

public class Mother extends Thread{
	private ArrayList<dumpling> list;
	public Mother(ArrayList<dumpling> list) {
		this.list = list;
	}
	public void run() {
		synchronized (list) {
			while(true){
				if(list.size()==0){
					try {
						list.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				dumpling du = list.remove(0);
				System.out.println("吃了"+du.name);
				list.notify();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}

package pass;

import java.util.ArrayList;

public class Father extends Thread{
	private ArrayList<dumpling> list;
	public Father(ArrayList<dumpling> list) {
		this.list = list;
	}
	public void run() {
		int count = 0;
		while(true){
			synchronized (list) {
			if(list.size()>0){
				try {
					list.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			dumpling du = new dumpling("饺子个数"+count);
			list.add(du);
			count++;
			list.notify();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
  }
}

package pass;

public class dumpling {
	public String name;
	public dumpling(String name) {
		this.name = name;
	}
}


上述代码打印输出为:吃了饺子个数0

吃了饺子个数1
吃了饺子个数2
吃了饺子个数3、、、、、

这种为生产/消费模型。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值