一、程序、进程、线程的概念
1、程序
指的是使用某种语言编写的一组指令的集合,主要是为了完成某种任务。
2、进程
正在执行的一个程序,如运行360软件等,就是一个进程。
进程是资源分配的单位,系统在运行时,会为每个进程分配不同的内存区域。
3、线程
指的程序内部执行的一条路径,每个线程都拥有独立的运行栈和程序技术器,线程之间的切换开销小。
进程可以细化为多个线程。
每个线程,拥有自己独立的:栈、程序计数器
多个线程,共享同一个进程中的结构:方法区、堆。
二、并发与并行概念
1、并发
并发指的是,单个cpu做一件事。如:秒杀活动,多个人同时抢购。
2、并行
是指多个cpu,分别做不同的事情。
三、线程的四种创建方式
思考:针对不同方法的优缺点
package com.ma.java;
import java.util.concurrent.*;
/*
* 线程的四种编写方法
* 1、继承的方式
* */
class myThread extends Thread{
@Override
public void run( ) {
for (int i = 0; i <100 ; i++) {
if (i%2==0){
// 静态方法
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +": "+ i);
}
if(i%20==0){
yield();
}
}
}
}
/*
* 线程的四种编写方法
* 2、实现接口方式 重写run
* */
class myThread2 implements Runnable{
public void run(){
for (int i = 0; i <100 ; i++) {
if (i%2==0){
System.out.println(Thread.currentThread().getName() +": "+ i);
}
if(i%20==0){
System.out.println(i);
}
}
}
}
/*
* 线程的四种编写方法
* 3、实现接口Callable方式 重写run jdk1.5
* */
class myThread3 implements Callable{
@Override
public Object call() throws Exception{
int sum=0;
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+":" + i);
sum++;
}
return sum;
}
}
/*
* 线程的四种编写方法
* 4、通过线程池 jdk1.5
* 看main
* */
public class ThreadTest {
public static void main(String[] args) {
System.out.println("继承方式********************************************************");
myThread t1 = new myThread();
t1.setName("继承Thread 线程:");
t1.start();
System.out.println("Runnable********************************************************");
myThread2 t2 = new myThread2();//获取Runnable对象
/*线程源码 可接受Runnable对象
* public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
* */
Thread t2_1 = new Thread(t2);
t2_1.setName("Runnable 线程:");
t2_1.start();
System.out.println("Runnable********************************************************");
/* 1 创建callable 实现类
2、 将实现类的对象作为传递到FutureTask构造器中,创建FutureTask的对象
3、将实现类对象传到FutureTask的对象
//4.获取Callable中call方法的返回值
//5 get()返回值即为FutureTask构造器参数Callable实现类重写的call()的返回值。
* */
myThread3 t3 = new myThread3();
FutureTask futureTask = new FutureTask(t3);
new Thread(futureTask).start();
try {
Object sum = futureTask.get();
System.out.println("总和为:" + sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("线程池********************************************************");
/*1、 指定线程池数量的线程池
*
*
* */
ExecutorService service = Executors.newFixedThreadPool(10);
// ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
service.execute(new myThread2());//适合适用于Runnable
service.execute(new myThread2());//适合适用于Runnable
// service.submit(Callable callable);//适合使用于Callable
service.shutdown();
// 主线程
for (int i = 0; i <100 ; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ":******* " + i);
}
}
String s1 ="";
}
}
四、线程的状态转换及常用方法
五、 线程的同步机制
三种方式 分别同步方法、同步代码块、锁
package com.ma.java;
import java.util.concurrent.locks.ReentrantLock;
/*线程的安全机制
*多窗口买票案例
* 如下会出现线程安全问题
* */
//方法1 synchronized 同步代码块
class testThread extends Thread{
private static int count =100;
public void run(){
/* 注意这里使用唯一对象作为同步监视器,因为是继承方法创建线程无法使用this
* */
synchronized(testThread.class) {
while (true) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票 :" + count);
count--;
}
}
}
}
class testThread_1 extends Thread {
private static int count = 100;
public void run() {
/* 注意这里使用唯一对象作为同步监视器,因为是继承方法创建线程无法使用this
* */
while (true) {
show();
}
}
private static synchronized void show(){
if(count>0){
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currentThread().getName() + "售票 :" + count);
count--;
}
}
}
class testThread2 implements Runnable{
private static int count =100;
public void run(){
/* 注意这里使用唯一对象作为同步监视器,因为是继承方法创建线程无法使用this
* */
while(true) {
synchronized(this){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count>0) {
System.out.println(Thread.currentThread().getName() + "售票 :" + count);
count--;
}else{
break;
}
}
}
}
}
//方法2 synchroized 同步方法 对方法修饰synchronized 默认同步监视器this,
//注意 当使用 继承方式线程,使用class类作为同步方法,或者static
class testThread3 implements Runnable{
private static int count =100;
public synchronized void run(){
/* 注意这里使用唯一对象作为同步监视器,因为是继承方法创建线程无法使用this
* */
while(true){
try {
wait(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count>0) {
System.out.println(Thread.currentThread().getName() + "售票 :" + count);
count--;
}else{
break;
}
}
}
}
//方法3 通过锁的方式 jdk 5
class testThread4 implements Runnable{
private static int count =100;
private ReentrantLock lock = new ReentrantLock();
public void run(){
/* 注意这里使用唯一对象作为同步监视器,因为是继承方法创建线程无法使用this
* */
while(true) {
try {
lock.lock();
if (count > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票 :" + count);
count--;
} else {
break;
}
}finally {
lock.unlock();
}
}
}
}
public class SynThread {
public static void main(String[] args) {
// 通过继承方式 --同步代码块
// testThread t1 = new testThread();
// testThread t2 = new testThread();
// testThread t3 = new testThread();
// 通过继承方式 --同步方法
// testThread_1 t1 = new testThread_1();
// testThread_1 t2 = new testThread_1();
// testThread_1 t3 = new testThread_1();
// t1.setName("线程1");
// t2.setName("线程2");
// t3.setName("线程3");
// t1.start();
// t2.start();
// t3.start();
// Runnable
// testThread2 runt1 = new testThread2();//同步代码块
// testThread3 runt1 = new testThread3();//同步方法
testThread4 runt1 = new testThread4();//锁的方式
Thread t1 = new Thread(runt1);
Thread t2 = new Thread(runt1);
Thread t3 = new Thread(runt1);
t1.setName("Runnable 线程1");
t2.setName("Runnable 线程2");
t3.setName("Runnable 线程3");
t1.start();
t2.start();
t3.start();
}
}
六、 消费者、生产者
package com.ma.java;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
/*生产者、消费者问题
* */
class Clerk{
static int productCount = 0;
public synchronized void produceProduct(){
if(productCount <20){
productCount ++;
System.out.println(Thread.currentThread().getName() +"生产者开始生产第:"+productCount+"商品");
notify();
}else{
try {
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
public synchronized void consumeProduct(){
if(productCount >0){
System.out.println(Thread.currentThread().getName()+"消费者开始消费第"+productCount+"商品");
productCount--;
notify();
}else{
try{
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
class Producer extends Thread{
private Clerk clerk;
public Producer(Clerk clerk) {
this.clerk = clerk;
}
public void run(){
System.out.println(getName() + ":开始生产产品.....");
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
clerk.produceProduct();
}
}
}
class Consumer extends Thread{
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
public void run(){
System.out.println(getName() +"开始消费");
while (true){
try{
Thread.sleep(20);
}catch (InterruptedException e){
e.printStackTrace();
}
clerk.consumeProduct();
}
}
}
public class ProductTest {
public static void main(String[] args) {
Clerk clerk = new Clerk();
Producer p1 = new Producer (clerk);
p1.setName("生产者1");
Consumer c1 = new Consumer(clerk);
c1.setName("消费者1");
Consumer c2 = new Consumer(clerk);
c2.setName("消费者2");
p1.start();
c1.start();
c2.start();
}
}
clerk相当于共享数据池
七、常用方法
- 面试题:sleep() 和 wait()的异同?
- 1.相同点:一旦执行方法,都可以使得当前的线程进入阻塞状态。
- 2.不同点:
- 1)两个方法声明的位置不同:Thread类中声明sleep() , Object类中声明wait()
- 2)调用的要求不同:sleep()可以在任何需要的场景下调用。 wait()必须使用在同步代码块或同步方法中
- 3)关于是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep()不会释放锁,wait()会释放锁。
notify():一旦执行此方法,就会唤醒被wait的一个线程。如果有多个线程被wait,就唤醒优先级高的那个。
notifyAll():一旦执行此方法,就会唤醒所有被wait的线程。
1.wait(),notify(),notifyAll()三个方法必须使用在同步代码块或同步方法中。
2.wait(),notify(),notifyAll()三个方法的调用者必须是同步代码块或同步方法中的同步监视器。 否则,会出现IllegalMonitorStateException异常
3.wait(),notify(),notifyAll()三个方法是定义在java.lang.Object类中。