多线程
进程:是一个正在执行中的程序。
每一个进程执行都有一个执行顺序。该顺序是一个执行路径,或者叫一个控制单元。
线程:就是进程中的一个独立的控制单元。
线程在控制着进程的执行。
多线程:一个进程中至少有一个线程。
创建线程的第一种方式:继承Thread类
步骤:
1,定义类继承Thread。
2,复写Thread类中的run方法。
目的:将自定义代码存储在run方法。让线程运行。
3,调用线程的start方法,
该方法两个作用:启动线程,调用run方法。
class Demo extendsThread
{
public void run()
{
for(intx=0; x<60; x++)
System.out.println("demorun----"+x);
}
}
class ThreadDemo
{
public static void main(String[] args)
{
//for(intx=0; x<4000; x++)
//System.out.println("HelloWorld!");
Demod = new Demo(); //创建好一个线程。
//d.start(); //开启线程并执行该线程的run方法。
d.run(); //仅仅是对象调用方法。而线程创建了,并没有运行。
for(intx=0; x<60; x++)
System.out.println("HelloWorld!--"+x);
}
}
创建线程的第二种方式:实现Runnable接口
步骤:
1,定义了实现Runnable接口。
2,覆盖接口的run方法。将多线程要运行的代码存入其中。
3,创建Thread类的对象(创建线程),并将Runnable接口的子类对象作为参数传递给Thread的构造函数。
为什么要传递?因为线程要运行的代码都在Runnable子类的run方法中存储。所以要将该run方法所属的对象
传递给Thread。让Thread线程去使用该对象调用其run方法。
4,调用Thread对象的start方法。开启线程。
这种方法避免了单继承的局限性,所以推荐用这个方法
public classRunnableDemo implements Runnable {
public void run() {
for(int x =0;x<10;x++){
System.out.println(Thread.currentThread().getName()+" "+x);
}
}
public static void main(String[] args) {
RunnableDemo rd = new RunnableDemo();
Thread t1 = new Thread(rd);
Thread t2 = new Thread(rd);
t1.start();
t2.start();
}
}
在学习多线程的时候我们还学习了一种设计模式叫做单例设计模式
这是单例设计模式的两种形式:
单例设计模式
特点:只能有一个对象,自己建立对象、、、
(1)饿汉式(一进来就创建对象)
class Single
{
private static final Single s =new Single;
private Single(){}
public static SinglegetInstance()
{
return s;
}
}
(2)懒汉式
class Single2
{
private static Single s = null;
private Single(){}
public static SinglegetInstance()
{
if(s==null)
{
s = new Single;
}
return s;
}
}
*两者区别就是:饿汉式一开始就创建对象并实例化,而懒汉式则是使用的时候创建对象
最后,多线程的最大弊端就是安全问题
JDK1.5之前我们用的是Synchronized
JDK1.5 中提供了多线程升级解决方案。
将同步Synchronized替换成现实Lock操作。
将Object中的wait,notifynotifyAll,替换了Condition对象。
该对象可以Lock锁 进行获取。
这面这个生产者和消费者的示例展现了以上特点:
importjava.util.concurrent.locks.*;
classProducerConsumerDemo
{
public static void main(String[] args)
{
Resourcer = new Resource();
Producerpro = new Producer(r);
Consumercon = new Consumer(r);
Threadt1 = new Thread(pro);
Threadt2 = new Thread(pro);
Threadt3 = new Thread(con);
Threadt4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
/*
Lock:替代了Synchronized
lock
unlock
newCondition()
Condition:替代了Object wait notify notifyAll
await();
signal();
signalAll();
*/
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
// t1 t2
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();//t1,t2
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 Producerimplements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res= res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch (InterruptedException e)
{
}
}
}
}
class Consumerimplements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res= res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch (InterruptedException e)
{
}
}
}