您的位置:首页 > 编程语言 > Java开发

Java的多线程机制下

2015-07-04 22:16 447 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------


多线程的同步:

1、线程安全:

当多个线程去访问同一个资源时,会引发一些安全问题。为了解决这样的问题,需要实现多线程的同步,即限制某个资源在同一时刻只能被一个线程访问。

package com.test.xiancheng;

public class XCanquan {
public static void main(String[] args) {
SafeThread st = new SafeThread();
new Thread(st,"线程一").start();;
new Thread(st,"线程二").start();;
new Thread(st,"线程三").start();;
new Thread(st,"线程四").start();;
}

}
class SafeThread implements Runnable
{
private int tickets = 10;
public void run()
{
while(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---卖出的票:"+tickets--);
}
}
}


2:、线程同步

package com.test.xiancheng;

public class XCtongbufangfa {
/**
* * 多线程同步:
* 1、线程安全
* 2、线程同步方法
* 3、同步方法:当把共享资源的操作放在synchronized定义的区域内时,便为这些操作加了同步锁
*              在方法前面同样可以使用synchronized关键字来修饰,被修饰的方法为同步方法,它能实现
*              和同步代码块同样的功能。被synchronized修饰的方法在某一时刻只允许一个线程访问
*              ,访问该方法的其他线程都会被阻塞,直到当前线程访问完毕后,其他线程才有机会执行方法
*
* @param args
*/
public static void main(String[] args) {
Ticket3 t = new Ticket3();
new Thread(t,"线程一").start();
new Thread(t,"线程二").start();
new Thread(t,"线程三").start();
new Thread(t,"线程四").start();
}

}
class Ticket3 implements Runnable
{
private int tickets = 10;
public void run()
{
while(true)
{
saleTicket();
if(tickets<0)
break;
}
}
private synchronized void saleTicket()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"---卖出的票"+tickets--);
}
}
}

3、线程的死锁问题

两个线程在运行时都在等待对方的锁,这样便造成了程序的停滞,这种现象称为死锁。

package com.test.xiancheng;

public class XCsisuo {
public static void main(String[] args) {
DeadLoackThread d1 = new DeadLoackThread(true);
DeadLoackThread d2 = new DeadLoackThread(false);
new Thread(d1,"Chinese").start();
new Thread(d2,"American").start();
}

}
class DeadLoackThread implements Runnable
{
static Object chopsticks = new Object();
static Object knifAndFork = new Object();
private boolean flag;
DeadLoackThread(boolean flag)
{
this.flag=flag;
}
public void run()
{
if(flag)
{
while(true)
{
synchronized (chopsticks)
{
System.out.println(Thread.currentThread().getName()+"---if--chopsticks");
synchronized (knifAndFork)
{
System.out.println(Thread.currentThread().getName()+"---if--knifAndFork");
}
}
}
}
else
{
while(true)
{
synchronized (knifAndFork)
{
System.out.println(Thread.currentThread().getName()+"---else---knifAndFork");
synchronized (chopsticks)
{
System.out.println(Thread.currentThread().getName()+"---else---chopsticks");
}
}
}
}
}
}


4、多线程通信

在多线程的程序中,上下工序可以看作两个线程,这两个线程之间需要协同完成工作,就需要线程之间进行通信。

案例说明:

package com.test.xiancheng;

public class Storage {
private int[] cells = new int[10];
private int inPos,outPos;
private int count;
public synchronized void put(int num)
{
try
{
while(count==cells.length)
{
this.wait();
}
cells[inPos] = num;
System.out.println("在cells["+inPos+"]中放入数据--"+cells[inPos]);
inPos++;
if(inPos==cells.length)
{
inPos=0;
}
count++;
this.notify();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public synchronized void get()
{
try
{
while(count==0)
{
this.wait();
}
int data = cells[outPos];
System.out.println("从cells["+outPos+"]中取出数据"+data);
outPos++;
if(outPos==cells.length)
outPos=0;
this.notify();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}


package com.test.xiancheng;

public class Input implements Runnable
{
private Storage st;
private int num;
Input(Storage st)
{
this.st=st;
}
public void run()
{
while(true)
{
st.put(num++);
}
}

}


package com.test.xiancheng;

public class Output implements Runnable
{
private Storage st;
Output(Storage st)
{
this.st=st;
}
public void run()
{
while(true)
{
st.get();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: