程序
是一个指令的集合
进程
(正在执行中的程序)是一个静态的概念
进程是程序的一次静态执行过程,占用特定的地址空间每个进程都是独立的,由三部分组成
cpudatacode 缺点
内存的浪费cpu的负担
线程
进程中的一个“单一的连续控制流程”
线程又被轻量级进程多个线程之间可能会相互之间有影响一个进程可能会拥有多个并行的线程一个进程中的线程共享一个内存单元,可以访问相同的变量和对象,而且他们从同一堆中分配对象由于线程间的通信是在同一地址空间上进行的,所以不需要额外的通信机制,这就使得通信更简便而且信息传递的速度也更快 进程与线程的区别
java中实现多线程在java中负责线程这个功能的类:Java.lang.Thread可以通过创建Thread的实例来创建新的线程每个线程都是通过某个特定Thread对象所对应的方法run()来完成 *** 作,方法run()被称为线程体通过调用Thread类的start()方法来启动一个线程 创建线程的方式
1.继承Thread
package Demo; public class ThreadDemo extends Thread{ @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"-----------"+i); } } public static void main(String[] args) { ThreadDemo threadDemo = new ThreadDemo(); threadDemo.start(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"=========="+i); } } }
第一次运行结果
第二次运行结果
2.实现Runnable接口
package Demo; public class RunnableDemo implements Runnable{ @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"-----------"+i); } } public static void main(String[] args) { RunnableDemo runnableDemo = new RunnableDemo(); Thread thread = new Thread(runnableDemo); thread.start(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"=========="+i); } } }
两种事项实现方式的对比
推荐使用第二种方法,java是单继承,将继承关系留给最需要的类
案例思考:卖票程序package Demo; public class TicketRunnable implements Runnable{ private int ticket = 5; @Override public void run() { for(int i=0;i<100;i++){ if(ticket>0){ System.out.println(Thread.currentThread().getName()+"正在出售第"+(ticket--)+"张票"); } } } public static void main(String[] args) { TicketRunnable ticketRunnable = new TicketRunnable(); Thread t1 = new Thread(ticketRunnable); Thread t2 = new Thread(ticketRunnable); Thread t3 = new Thread(ticketRunnable); Thread t4 = new Thread(ticketRunnable); t1.start(); t2.start(); t3.start(); t4.start(); } }
思考:数据错乱的情况该如何解决?
线程的代理设计模式程序参考《设计模式之禅》
1.接口
package proxy; public interface KindWoman { public void makeEyesWithMen(); public void playWithMen(); }
2.代理人
package proxy; public class WangPo implements KindWoman{ private KindWoman kindWoman; public WangPo(){ this.kindWoman = new PanJinLian(); } public WangPo(KindWoman kindWoman) { this.kindWoman = kindWoman; } @Override public void makeEyesWithMen() { this.kindWoman.makeEyesWithMen(); } @Override public void playWithMen() { this.kindWoman.playWithMen(); } }
3.被代理人
package proxy; public class PanJinLian implements KindWoman{ @Override public void makeEyesWithMen() { System.out.println("潘金莲在抛媚眼"); } @Override public void playWithMen() { System.out.println("潘金莲....."); } }
4.测试类
package proxy; public class XiMenQing { public static void main(String[] args) { WangPo wangPo = new WangPo(); wangPo.playWithMen(); wangPo.makeEyesWithMen(); } }
运行截图
我们可以发现,虽然我们调用的是wangPo的方法,但是实际实现的是潘金莲这个对象的内容
代理模式主要使用了 Java 的多态,干活的是被代理类,代理类主要是接活,你让我干活,好,我交给幕后的类去干,你满意就成,那怎么知道被代理类能不能干呢?同根就成,大家知根知底,你能做啥,我能做啥都清楚的很,同一个接口呗。
线程状态线程 *** 作的相关方法
方法1-5案例
public class ThreadApiDemo implements Runnable{ public static void main(String[] args) { //获取当前线程对象 Thread thread = Thread.currentThread(); //获取当前线程的名称 System.out.println(thread.getName()); //获取线程的id System.out.println(thread.getId()); //获取线程的优先级(在一般系统中范围是0-10,如果没有经过设置,默认为5) System.out.println(thread.getPriority()); //设置线程池的优先级 thread.setPriority(6); System.out.println(thread.getPriority()); ThreadApiDemo threadApiDemo = new ThreadApiDemo(); Thread t1 = new Thread(threadApiDemo); System.out.println(t1.isAlive());//false t1.start(); System.out.println(t1.isAlive());//true System.out.println(t1.getPriority()); System.out.println(t1.isAlive());//true } @Override public void run() { } }
方法6案例
public class MyRun implements Runnable{ @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"-------"+i); } } }
public class JoinTest { public static void main(String[] args) { MyRun myRun = new MyRun(); Thread thread = new Thread(myRun); thread.start(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"========"+i); if(i==3){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
方法7案例
public class MyRun implements Runnable{ @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"-------"+i); } } }
public class SleepTest { public static void main(String[] args) { MyRun myRun = new MyRun(); Thread thread = new Thread(myRun); thread.start(); for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"========="+i); if(i==2){ try { Thread.sleep(5000);//休眠5s } catch (InterruptedException e) { e.printStackTrace(); } } } } }
注意:
在多线程的时候,可以实现唤醒和等待的功能,但是唤醒和等待 *** 作对应的不是thread对象,而是我们设置的共享变量
案例思考:双线程输出顺序问题代码实现
package execcise; public class Test1 implements Runnable{ @Override public void run() { for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"-------"+i); try { Thread.sleep(1050); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Test1 test1 = new Test1(); Thread thread = new Thread(test1); thread.start(); for(int i=10;i>0;i--){ System.out.println(Thread.currentThread().getName()+"======="+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
运行截图
多线程的安全性问题引例:案例思考:卖票程序
同步代码块package Demo; public class TicketRunnable implements Runnable{ private int ticket = 5; @Override public void run() { for(int i=0;i<100;i++){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this){ if(ticket>0){ System.out.println(Thread.currentThread().getName()+"正在出售第"+(ticket--)+"张票"); } } } } public static void main(String[] args) { TicketRunnable ticketRunnable = new TicketRunnable(); Thread t1 = new Thread(ticketRunnable,"A"); Thread t2 = new Thread(ticketRunnable,"B"); Thread t3 = new Thread(ticketRunnable,"C"); Thread t4 = new Thread(ticketRunnable,"D"); t1.start(); t2.start(); t3.start(); t4.start(); } }同步方法
package Demo; public class TicketRunnable implements Runnable{ private int ticket = 5; @Override public void run() { for(int i=0;i<100;i++){ try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } sale(); } } public synchronized void sale(){ if(ticket>0){ System.out.println(Thread.currentThread().getName()+"正在出售第"+(ticket--)+"张票"); } } public static void main(String[] args) { TicketRunnable ticketRunnable = new TicketRunnable(); Thread t1 = new Thread(ticketRunnable,"A"); Thread t2 = new Thread(ticketRunnable,"B"); Thread t3 = new Thread(ticketRunnable,"C"); Thread t4 = new Thread(ticketRunnable,"D"); t1.start(); t2.start(); t3.start(); t4.start(); } }死锁
同步可以保证资源共享 *** 作的正确性,但是过多同步也会产生死锁死锁一般情况下表示互相等待,是程序运行时出现的一种问题 线程的生产者和消费者问题
举例
经典代码实现1.生产者
package pc3; public class Producer implements Runnable { private Goods goods; public Producer(Goods goods) { this.goods = goods; } @Override public void run() { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { goods.set("娃哈哈","矿泉水"); } else { goods.set("旺仔","小馒头"); } } } }
2.消费者
package pc3; public class Consumer implements Runnable { private Goods goods; public Consumer(Goods goods) { this.goods = goods; } @Override public void run() { for(int i = 0;i<10;i++){ goods.get(); } } }
3.对象
package pc3; public class Goods { private String brand; private String name; //默认是不存在商品的,如果值等于true的话,代表有商品 private boolean flag = false; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getName() { return name; } public void setName(String name) { this.name = name; } //消费者获取商品 public synchronized void get(){ if (!flag){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("消费者取走了"+this.getBrand()+"----"+this.getName()); flag = false; //唤醒生产者去进行生产 notify(); } //生产者生产商品 public synchronized void set(String brand,String name){ //当生产者抢占到cpu资源之后会判断当前对象是否有值,如果有的话,以为着消费者还没有消费,需要提醒消费者消费,同时 //当前线程进入阻塞状态,等待消费者取走商品之后,再次生产,如果没有的话,不需要等待,不需要进入阻塞状态,直接生产即可 if(flag){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.setBrand(brand); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } this.setName(name); System.out.println("生产者生产了" + this.getBrand() + "--" + this.getName()); //如果代码执行到此处,意味着已经生产完成,需要将flag设置为true flag = true; //唤醒消费者去进行消费 notify(); } }
4.测试类
package pc3; public class Test { public static void main(String[] args) { Goods goods = new Goods(); Producer producer = new Producer(goods); Consumer consumer = new Consumer(goods); Thread t1 = new Thread(producer); Thread t2 = new Thread(consumer); t1.start(); t2.start(); } }JVC方式(先了解即可)
1.生产者
package pc4; import java.util.concurrent.BlockingQueue; public class ProduceQueue implements Runnable{ private BlockingQueueblockingQueue; public ProduceQueue(BlockingQueue blockingDeque) { this.blockingQueue = blockingDeque; } @Override public void run() { for(int i=0;i<10;i++){ Goods goods = null; if(i%2==0){ goods = new Goods("娃哈哈","矿泉水"); }else { goods = new Goods("旺仔","小馒头"); } System.out.println("生产者开始生产商品"+goods.getBrand()+goods.getName()); try { blockingQueue.put(goods); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2.消费者
package pc4; import java.util.concurrent.BlockingQueue; public class ConsumerQueue implements Runnable{ private BlockingQueueblockingQueue; public ConsumerQueue(BlockingQueue blockingDeque) { this.blockingQueue = blockingDeque; } @Override public void run() { for(int i=0;i<10;i++){ try { Goods goods = blockingQueue.take(); System.out.println("消费者消费的商品是"+goods.getBrand()+goods.getName()); } catch (InterruptedException e) { e.printStackTrace(); } } } }
3.对象
package pc4; public class Goods { private String brand; private String name; public Goods(String brand, String name) { this.brand = brand; this.name = name; } //默认是不存在商品的,如果值等于true的话,代表有商品 private boolean flag = false; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.测试类
package pc4; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class Test { public static void main(String[] args) { BlockingQueueblockingQueue = new ArrayBlockingQueue(5); ProduceQueue produceQueue = new ProduceQueue(blockingQueue); ConsumerQueue consumerQueue = new ConsumerQueue(blockingQueue); new Thread(produceQueue).start(); new Thread(consumerQueue).start(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)