------- android培训、java培训、期待与您交流! ----------
IO流
进程:是指内存中正在执行的一个程序。每一个进程执行都有一个执行顺序。该顺序是一个执行路径或者一个控制单元。其中一个进程可以有多个线程。例如一个证在运行的**.exe就是一个进程。
线程:是指进程中的一个控制单元,一个进程中可以有多个线程,多个线程之间可以共享数据。
比如java VM运行的时候会有一个进程java.exe.
该进程中至少有一个线程负责java程序的执行。而且这个线程运行的代码存在于main方法中,该线程称之为主线程。
其实jvm启动不止一个主线程,还有一个负责垃圾回收机制的线程。
那么如何创建一个线程呢?
第一种方式:
1、定义类继承Thread类
2、覆写Thread类中的run方法。目的:将自定义代码存储在run方法中。让线程运行。
3、调用线程的start方法。该方法有两个作用:启动线程,调用run方法。
<span style="font-size:18px;">class Demo1 extends Thread
{
public void run()
{
for(int x=0; x<60; x++)
System.out.println("demo run----"+x);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
Demo1 d = new Demo1();//创建好一个线程。
d.start();//开启线程并执行该线程的run方法。
for(int x=0; x<60; x++)
System.out.println("Hello World!--"+x);
}
}</span>
经多次输出测试:主线程和Demo1线程随机交替运行
总结:从每次运行可以看到每次的结果都不一样。
这是因为多个线程都获取cpu的执行权。cpu执行到随,随就运行。
明确一点,在某一个时刻,只能有一个程序在运行。(多核除外)
cpu在快速的做着切换,以达到看上去是同时运行的效果。
我们可以形象的把多线程的运行行为在互相抢夺cpu的执行权。
这就是多线程的一个特性:随机性。谁抢到,随执行。至于执行多久,cpu说的算。
第二种创建方式:
1、定义类实现Runnable接口。
2、覆盖Runnable接口中的run方法。
将线程要运行的代码放在run方法中
3、通过Thread类建立线程对象。
4、将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数
原因:自定义的run方法所属的对象时Runnable接口的子类对象。
所以要让线程去指定制定对象的run方法。就必须明确该run方法所属对象。、
5、调用Thread类的Start方法开启线程并调用Runnable接口子类的run方法。
class Ticket implements Runnable//extends Thread
{
private int tick = 100;
public void run()
{
System.out.println("run of Runnable is run");
}
}
class TicketDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);//创建了一个线程;
t1.start();
}
}
输出:run of Runnable is run
两种创建方式的比较
A extends Thread:
单继承的局限性决定了该方式的类不能再继承
同份资源不共享
A implements Runnable:
该类不仅可以继承其它类,还能实现更多的接口,功能扩展性强
多个线程可以共享一个资源,方便多线程处理同一份资源
线程生命周期:
NEW-------新建状态,至今尚未启动的线程处于这种状态。
RUNNABLE-------运行状态,正在 Java 虚拟机中执行的线程处于这种状态。
BLOCKED-------阻塞状态,受阻塞并等待某个监视器锁的线程处于这种状态。
WAITING-------冻结状态,无限期地等待另一个线程来执行某一特定操作的线程处于这种状态。
TIMED_WAITING-------等待状态,等待另一个线程来执行取决于指定等待时间的操作的线程处于这种状态。
TERMINATED-------已退出的线程处于这种状态。
多线程安全问题:
我们先来看看下面一段代码:
class Bank
{
private int sum;
public void add(int n)
{
sum = sum + n;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println("sum="+sum);
}
}
class Cus implements Runnable
{
private Bank b = new Bank();
public void run()
{
for(int x=0; x<3; x++)
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
输出:sum=100
sum=300
sum=400
sum=500
sum=600
sum=600
解决办法:
1、同步代码块
synchronized(对象)//该对象表示一个同步监视器
{
//需要被同步的代码
}
代码实现:
class Bank
{
private int sum;
Object obj = new Object();
public void add(int n)
{
synchronized(obj)
{
sum = sum + n;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println("sum="+sum);
}
}
}
class Cus implements Runnable
{
private Bank b = new Bank();
public void run()
{
for(int x=0; x<3; x++)
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
输出:sum=100
sum=200
sum=300
sum=400
sum=500
sum=600
2、同步方法
synchronized 返回值类型方法名(参数列表)
{
/**
同步代码块
*/
}
下面我们看代码实现:
class Bank
{
private int sum;
public synchronized void add(int n)
{
sum = sum + n;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println("sum="+sum);
}
}
class Cus implements Runnable
{
private Bank b = new Bank();
public void run()
{
for(int x=0; x<3; x++)
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
}
}
输出:sum=100
sum=200
sum=300
sum=400
sum=500
sum=600
同步函数到底用的哪一个锁呢?
其实函数都有一个所属对象引用。那就是This
下面我们来用代码验证一下:
class Ticket implements Runnable
{
private int tick = 100;
Object obj = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(this)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
}
}
}
}
else
while(true)
show();
}
public synchronized void show()//this
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
}
}
}
class ThisLockDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
由输出结果我们可以分析出同步方法的锁就是this对象
3、静态同步方法
如果同步函数被静态修饰后,使用的锁是什么呢?
静态进内存是,内存中没有本类对象,但是一定有该类对应的字节码文件对象。
类名.class 该对象的类型是Class
静态的同步方法,使用的锁是该方法所在类的字节码文件对象。 类名.class
class Ticket implements Runnable
{
private static int tick = 100;
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(Ticket.class)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
}
}
}
}
else
while(true)
show();
}
public static synchronized void show()
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
}
}
}
class StaticMethodDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
单例设计模式
饿汉式
class Single
{
private static final Single s = new Single();
private Single(){}
public static Single getInstance()
{
return s;
}
}
懒汉式
class Single
{
private static Single s = null;
private Single(){}
public static Single getInstance()
{
if(s==null)
{
synchronized(Single.class)
{
if(s==null)
s = new Single();
}
}
return s;
}
}
同步锁
JDK1.5 中提供了多线程升级解决方案。
将同步中的synchronized替换成现实lock操作。
将object中的wait、notify、notifyAll,替换成了Condition对象。
该对象可以用lock锁进行获取。
import java.util.concurrent.locks.*;
class ProducerConsumerDemo2
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock();//释放锁的动作一定要执行。
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch (InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}
}