您的位置:首页 > 职场人生

黑马程序员_java基础多线程

2015-04-25 16:26 369 查看
------ <a href="http://www.itheima.com" target="blank">Windows Phone 7手机开发</a>、<a href="http://www.itheima.com" target="blank">.Net培训</a>、期待与您交流! -------

/*多线程部分学习心得*/
//懒汉式 考点:1延迟加载2双重判断减少判断次数3synchronized的参数是类的字节码对象,非静态方法才使用默认的this锁
class single {

private static single getInstance()
{
if(s==null)
{
synchronized(single.class)
{
if(s==null)
s=new single();
}
}return s;
}
}
//写个死锁程序 考点:同步中嵌套同步,锁却不同。
class Ticket implements Runnable
{
private int tick = 100;
Object obj =new Object;
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(obj)
{
show();
}
}
}
else
while(true)
show();
}
public synchronized void show()
{
synchronized(obj)
{
if(tick>0)
{
try{Thread.sleep(10);}
catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...sale: "+ tick--);
}
}
}
}
class DeadLockDemo
{
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();
}
}
//线程间通讯:其实就是多个线程在操作同一个资源,但是操作的动作不同。
/*
* 思路:车一存在就有煤,所以车初始化就定义煤。有可能资源属性不完整就切换了进程。线程实现同步
* 的前题是1多(两)个线程共同操作资源的代码块要加上synchronized;2用的是同一把锁。
* 等待唤醒机制:为了满足输入一个取出一个,在资源中添加一个标记,判断为ture时,就冻结同时唤醒另一线程。
* 等待线程都存在线程池中的线程,notify通常唤醒第一个等待的线程。wait,notify,notifyAll都使用在同步
* 中,因为要对持有监视器(锁)的线程操作。
* 数据私有,提供方法。
* 生产者消费者,while循环,notifyAll
*
* 步骤
* 1 描述资源
* 2 描述输入的run
* 3 描述输出的run
*
*
* */
class ProducerConsumerDemo {
//主函数
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(pro);
Thread t4 = new Thread(pro);

t1.start();
t2.start();
t3.start();
t4.start();
}
}
//资源
class Resource {
private String name;
private int count = 1;
private boolean flag = false;

//方法要有自己的参数
public synchronized  void set(String name) {

while(flag)
//是真,就等待,注意格式
try{this.wait();}
catch(Exception e){}
//判断是否执行
//带有编号地赋值
//首先,写出构造方法,带线程名称更直观,产品名称
this.name = name+"--"+count++;
//力求代码简单
System.out.println(Thread.currentThread().getName()+"...生产者..."+name);
//唤醒程序
flag =true;
this.notifyAll();
}

//生产者

//消费者没有默认参数
public synchronized void out() {
while(!flag)
//是假,就等待,注意格式
try{this.wait();}
catch(Exception e){}
//判断是否执行
//带有编号地赋值
//首先,写出构造方法,带线程名称更直观,产品名称
//力求代码简单
System.out.println(Thread.currentThread().getName()+"...消费者....."+name);
//唤醒程序
flag =false;
this.notifyAll();
}
}

class Producer implements Runnable
{
private Resource res;

Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{//在不断设置值,类.方法名
res.set("+商品+");
}
}
}
class Consumer implements Runnable
{
private Resource res;

Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{//消费者在不断消费
res.out();
}
}
}
/**当没有指定的方法让冻结的线程恢复到运行状态时,

 * 这时需要对冻结进行清除。强制让线程恢复到运行状态。

 * Thread类提供了interrupt方法。

 * join抢夺cpu执行权。它结束了,主线程才恢复到执行状态。

 * join可临时加入线程执行。

 * t1.setPriority(Thread.MAX_PRIORITY);

 * Thread.yield();可稍微减缓线程的运行,具有平均执行的作用。

 * 单独封装成一个线程:独立运算,相互不相关,提高效率的方法。

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