关闭

多线程1

标签: 学习总结
72人阅读 评论(0) 收藏 举报
分类:

多线程是由于cpu的时分处理原理导致的技术

实际上是因为多个线程对同一个资源进行操作的技术

可以解决许多实际问题

但由此引发的问题是安全和死锁也需要相应的解决办法

 

主要有两种模型:

1 多个线程使用相同的操作操作同一个资源(卖票)

2 多个线程使用相反的操作操作同一个资源(生产者消费者)

 

卖票:

关键点:

1 ticket是共享资源 注意它的处理方式

2注意线程的安全问题

3注意while(true)和同步代码块的位置

 

class TicketDemo2

{

         publicstatic void main(String[] args)

         {

                   Tickett = new Ticket();

 

                   Threadt1 = new Thread(t);

                   Threadt2 = new Thread(t);

                   Threadt3 = new Thread(t);

                   Threadt4 = new Thread(t);

 

                   t1.start();

                   t2.start();

                   t3.start();

                   t4.start();

                           

                   System.out.println("-----------------");

                   System.out.println("MissionComplete!");

         }

}

 

class Ticket implements Runnable

{

         privateint ticket = 1000;  //注意 这里的ticket虽然没有static修饰 但也是共享数据

         //注意这个数字要搞大 否则cpu在切换之前就已经把票卖完了 你看不要想要的现象的

         //之前我搞成50 总是就一个窗口在卖票

 

         Objectobj = new Object();

 

         publicvoid run()

         {       

                   while(ticket>0)//这句也非常重要

                   //如果这句话在同步代码块里面 那么一个线程一旦进入了同步代码块 就会一直

                   //执行到结束(ticket==1)

                   //而这个线程永远都会是Thread0因为看似多线程同时执行实际上不过是先后执行

                   //因为代码的顺序是:

                   /*

                   t1.start();

                   t2.start();

                   t3.start();

                   t4.start();

                  

                   但如果这条语句不在同步代码块里面 而同步代码块里面除了输出语句和sleep方法以外

                   什么都没有的话

                   那么同步的代码将没有

                  

                   */

                   {

                            synchronized(obj)

                            {

//                                  if(ticket>0)  //注意这句话非常重要

                            //如果没有这句话 相当于同步代码块里面没有结束的信号

//                                  {

                                               try

                                               {

                                                        Thread.sleep(1);//注意这个数字不要搞大了

                                                        //对于cpu来说 1ms是很久一段时间

                                                        //搞大了以后 程序运行的总时间就会很长

                                                        //因为卖一次票就会睡1ms

                                                        //具体体现就是程序不是刷地出来一片 然后

                                                        //就结束了 而是明显看出来一条一条出来的

                                               }

                                               catch(Exception e)

                                               {

                                                        System.out.println("卧槽我错了");

                                               }

                                               System.out.println("从"+Thread.currentThread().getName()+

                                                        "号窗口:"+"卖出的车票编号"+ticket);

                                               ticket--;//

//                                  }

                            }       

                   }

         }

}

 

死锁实现:

关键点:

1两个不同的锁分别被两个不同的线程调用

2同步要嵌套

3 不一定每次运行都会出现

/*

需求:

死锁来一发

 

*/

 

class DeadLockDemo2

{

         publicstatic void main(String[] args) throws

InterruptedException

         {

                   Threadt1 = new Thread(new Test(true));

                   Threadt2 = new Thread(new Test(false));

 

                   t1.start();

                   t1.sleep(10);

                   t2.start();

         }

        

         publicstatic void sop(Object obj)

         {

                   System.out.println(obj);

         }

}

 

class MyLock

{

         staticObject locka = new Object();

         staticObject lockb = new Object();

}

 

class Test implements Runnable

{

         privateboolean flag;

 

         Test(booleanflag)

         {

                   this.flag= flag;

         }

 

         publicvoid run()

         {

                   if(flag == true)

                   {

                            while(true)

                            {

                                     synchronized(MyLock.locka)

                                     {

                                               sop("true&locka");

                                               synchronized

(MyLock.lockb)

                                               {

                                                        sop

("true&lockb");

                                               }

                                     }

                            }

                   }

                   else

                   {

                            while(true)

                            {

                                     synchronized(MyLock.lockb)

                                     {

                                               sop("false&lockb");

                                               synchronized

(MyLock.locka)

                                               {

                                                        sop

("false&locka");

                                               }

                                     }

                            }

                   }

         }

 

         publicstatic void sop(Object obj)

         {

                   System.out.println(obj);

         }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2862次
    • 积分:279
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档