首先了解进程:是一个可执行程序放在计算机存储器的一个指令序列,它是一个动态执行过程
1线程定义
类似是进程的子程序
2线程创建
分为继承Thread类
线程的创建和启动下面的代码实现
package com.jiedada.thread; //继承线程对象 class MyThreads extends Thread{ public MyThreads(String name) { super(name); } public void run() { for(int i=1;i<10;i++) { System.out.println(getName()+"这是第"+i+"次线程"); } } } public class ThreadOne { public static void main(String[] args) { // TODO Auto-generated method stub //创建线程对象 MyThreads my1=new MyThreads("线程1"); MyThreads my2=new MyThreads("线程2"); my1.start(); my2.start(); } }
和接口Runnable
为什么有类的线程却要再用runnalbe接口呢
1如果一个类继承了一个类,就不能继承Thread类了
2因为一些类只想使用run这一个方法
接口创建的类可以创建多个对象
接口类的定义,接口类的实列和start方法代码
package com.jiedada.thread; //接口类的创建 class RunTest implements Runnable{ @Override public void run() { // TODO Auto-generated method stub int i=1; while(i<10) { System.out.println(Thread.currentThread().getName()+"这是线程"+(i++)); } } } public class Test { public static void main(String[] args) { // TODO Auto-generated method stub //接口类的调用start方法,接口类发生的情况 RunTest r1=new RunTest(); Thread thread=new Thread(r1); thread.start(); RunTest r2=new RunTest(); Thread t1=new Thread(r2); t1.start(); } }
3线程的状态和生命周期
1线程的状态分为
创建
可运行
正在运行
sleep请求,是一个Thread的静态方法,在在使用的时候需要使用try catch来捕获程序终端异常
可以是用于计时器,一段时间的数据更新
package com.jiedada.thread; class SleepThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<15;i++) { System.out.println(Thread.currentThread().getName()+"这是线程"+i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class SleepTest { public static void main(String[] args) { // TODO Auto-generated method stub SleepThread s1=new SleepThread(); Thread t=new Thread(s1); t.start(); } }
join请求,join是一个最终的方法等待调用该线程结束后才能执行;
package com.jiedada.thread; class JoinT implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<15;i++) { System.out.println(Thread.currentThread().getName()+"这是一个线程"+i); } } } public class JoinThread { public static void main(String[] args) { // TODO Auto-generated method stub JoinT j=new JoinT(); Thread t=new Thread(j); t.start(); //这是一个join的方法里面可以放入时间参数 try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("这是主线程"); } }
阻塞
终止
线程优先级
Java为线程优先级提供了1-10的优先级,超出范围会抛出异常
主线程优先级为5
package com.jiedada.thread; class pThread extends Thread{ String name; public pThread(String name) { this.name=name; } public void run() { for(int i=0;i<10;i++) { System.out.println(getName()+"这是线程"+i); } } } public class youxianji { public static void main(String[] args) { // TODO Auto-generated method stub int sum=Thread.currentThread().getPriority(); System.out.println(sum); pThread p=new pThread("线程1"); pThread p1=new pThread("线程2"); p.setPriority(10); p1.setPriority(1); p.start(); p1.start(); } }
4线程调度
5同步与死锁
多线程运行的问题
1各个线程竞争CPU时间而获得运行时间
2各线程什么时候得到CPU时间,占用多久,是不可预测的
3一个正在运行着的线程在上面地方被暂停是不确定的
解决办法(同步)
用synchronized关键字来定义可以用于
1成员方法
2静态方法
3语句快
银行系统实列
bank类
package com.jiedada.bank; public class Bank { private String count; private int money; public Bank() { } public Bank(String count, int money) { super(); this.count = count; this.money = money; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "Bank [count=" + count + ", money=" + money + "]"; } //存入钱 public synchronized void SaveAccount() { int countmoney=this.money; countmoney+=200; try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setMoney(countmoney); System.out.println("存入后的钱为:"+countmoney); } //取钱操作 public synchronized void DrawAccount() { int countmoney=this.money; countmoney-=100; try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } setMoney(countmoney); System.out.println("取出钱后为:"+countmoney); } }
save money类
package com.jiedada.bank; //存钱 public class SaveMoney implements Runnable{ Bank bank; public SaveMoney(Bank bank) { this.bank=bank; } @Override public void run() { // TODO Auto-generated method stub bank.SaveAccount(); } }
draw money类
package com.jiedada.bank; public class DrawMoney implements Runnable { Bank bank; public DrawMoney(Bank bank) { this.bank=bank; } @Override public void run() { // TODO Auto-generated method stub bank.DrawAccount(); } }
test类
package com.jiedada.bank; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub //创建账户 Bank huang=new Bank("huangdada",1000); //创建线程 SaveMoney s=new SaveMoney(huang); Thread t1=new Thread(s); DrawMoney d=new DrawMoney(huang); Thread t2=new Thread(d); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(huang); } }
问腿
当银行中的钱不够时,我们就不能取出钱了。
举例生产者和消费者问题
qeuqe类
package com.jiedada.prodacanduse; public class Qeuqe { private int n; boolean flag=false; public synchronized int get() { if(!flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("取出的:"+n); flag=false; notifyAll(); return n; } public synchronized int set(int n) { if(flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("存入的:"+n); flag=true; notifyAll(); return this.n=n; } }
prodtion类
package com.jiedada.prodacanduse; public class Prodction implements Runnable{ Qeuqe qeuqe; public Prodction(Qeuqe qeuqe) { this.qeuqe=qeuqe; } @Override public void run() { // TODO Auto-generated method stub int i=0; while(true) { qeuqe.set(i++); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
user类
package com.jiedada.prodacanduse; public class User implements Runnable{ Qeuqe queqe; public User(Qeuqe qeuqe) { this.queqe=qeuqe; } @Override public void run() { // TODO Auto-generated method stub while(true) { queqe.get(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
test类
package com.jiedada.prodacanduse; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Qeuqe q1=new Qeuqe(); new Thread(new Prodction(q1)).start(); new Thread(new User(q1)).start(); } }