Java基础 5
一、多线程
1、 进程:在多任务的操作系统中,每个独立执行的程序称为进程,也就是“正在进行的程序”。
2、 线程一个进程可以包括一个或多个线程,一个线程就是一个程序内部的一条执行线索。
3、 用Thread.currutThread().getName();可以得到程序当前运行的线程的名字。
4、 多线程的产生过程
(1) 创建一个子类来覆盖Thread中的run()方法,就是在run方法中编写要执行的代码
(2) 用new Thread()创建一个线程,然后调用start()方法来启动线程
5、 后台线程和联合线程
(1) 某个线程对象在启动之前调用了setDeamon();方法,这个线程就成了后台线程
(2) pp.join()的作用是把所有的pp所对应的线程合并到用pp.join();语句的线程中
(3) java中只要有一个前台的线程在运行,线程就不会结束
二、使用Runnerable来创建线程的过程
1、 创建一个类TestThead来实现Runnerable接口
2、 实现Runnerable中的run()方法
3、 在main方法中编写 TestThread thread = new TestThread(); new Thread(thread).start();
三、模拟火车售票系统
其源代码是:
public class ThreadDemo1
{
public static void main(String[] args) throws InterruptedException
{
TestThread thread = new TestThread();
new Thread(thread).start();
Thread.sleep(2);
thread.string = new String("method");
new Thread(thread).start();
new Thread(thread).start();
new Thread(thread).start();
}
}
class TestThread implements Runnable//extends Thread
{
int tickets = 100;
String string = new String("");
public /*synchronized*/ void run()
{
//String string = new String();//这条语句不能放在run()中
if (string.equals("method"))
{
while(true)
{
sell();
}
}
else
{
while(true)
{
synchronized (this)//同步代码块的监视器是对象,所以,两者同步用this//对象
{
if(tickets>0)
{
try
{
Thread.sleep(100);
/* synchronized (this)
{
}*/
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("run1():"+Thread.currentThread().getName()+"is selling"+tickets--);
}
}
}
}
}
、 public synchronized void sell()//同步方法的监视器是this
{
if(tickets>0)
{
try
{
Thread.sleep(100);
/* synchronized (string)
{
}*/
{
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("run2():"+Thread.currentThread().getName()+"is selling"+tickets--);
}
}
}
里面实现了同步代码(synchronized)和同步块的同步。因为使用了同一个监视器的结果。
说明:我们对一个线程调用start()方法,并不是说这个线程马上开始执行,只是说这个线程处于启动状态,这个线程被启动后,不一定就在这个线程上执行
四、 死锁的问题
一个线程进入x监视器,另一个线程进入y监视器,x和y相互等待各自的监视器。
五、 线程间通信
1、 wait()方法通知当前的线程放弃监视器并进入睡眠状态,知道其他线程进入同一监视器并调用notify()为止。
2、 notify()方法用来唤醒同一对象监视器中调用wait()的第一个线程。
3、 线程生命的控制
如下图所示:
六、模拟生产者和消费者
源代码:
public class Lesson5_1
{
public static void main(String[] args)
{
/*Buffer1 buffer = new Buffer1();
new Thread(new Producer(buffer)).start();
new Thread(new Consumer(buffer)).start();*/
ThreadTest threadTest = new ThreadTest();
new Thread(threadTest).start();
for (int i = 0; i < 100; i++)
{
if(i==50)
threadTest.stopMe();
System.out.println("Thread is running");
}
}
}
class Producer implements Runnable
{
Buffer1 buffer;
public Producer(Buffer1 buffer )
{
this.buffer = buffer;
}
int i =0;
public void run()
{
while(true)
{
/
if(i==0)
{
try
{
buffer.putData("zhangsan", "male");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
try
{
buffer.putData("lisi", "female");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
i =(i+1)%2;
}
}
}
class Consumer implements Runnable
{
Buffer1 buffer;
public Consumer(Buffer1 buffer)
{
this.buffer = buffer;
}
public void run()
{
while(true)
{
buffer.get();
}
}
}
class Buffer1
{
private String name = "unknown";
private String sex = "unknown";
boolean bFull = false;
public synchronized void putData(String name,String sex) throws InterruptedException
{
if(bFull)
wait();
this.name = name;
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
this.sex = sex;
bFull = true;
notify();
}
public synchronized void get()
{
if(!bFull)
try
{
wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print(name);
System.out.println(":"+sex);
bFull =false;
notify();
}
}
class ThreadTest implements Runnable
{
private boolean bStop = false;
public void stopMe()
{
bStop = true;
}
public void run()
{
while(bStop)
{
System.out.println(Thread.currentThread().getName()+"is running");
}
}
}