zoukankan      html  css  js  c++  java
  • 关于JAVA线程练习题

    关于JAVA线程练习题

    1.写一个死锁:

     1 package javase.deadlock;
     2 /**
     3  * 死锁
     4  * @author yumu
     5  *
     6  */
     7 public class DeadLock {
     8 
     9     public static void main(String[] args) {
    10         Object o1=new Object();
    11         Object o2=new Object();
    12         //t1和t2共享两个线程
    13         Thread t1=new Mythread1(o1, o2);
    14         Thread t2=new Mythread1(o1, o2);
    15         t1.start();
    16         t2.start();
    17     }
    18 
    19 }
    20 class Mythread1 extends Thread{
    21     Object o1;
    22     Object o2;
    23     public Mythread1(Object o1,Object o2){
    24         this.o1=o1;
    25         this.o2=o2;
    26     }
    27     
    28     public void run(){
    29         synchronized (o1) {
    30             try {
    31                 Thread.sleep(1000);
    32             } catch (InterruptedException e) {
    33                 
    34                 e.printStackTrace();
    35             }
    36             synchronized (o2) {
    37                 
    38             }
    39         }
    40     }
    41 }
    42 class Mythread2 extends Thread{
    43     Object o1;
    44     Object o2;
    45     public Mythread2(Object o1,Object o2){
    46         this.o1=o1;
    47         this.o2=o2;
    48     }
    49     
    50     public void run(){
    51         synchronized (o2) {
    52             try {
    53                 Thread.sleep(1000);
    54             } catch (InterruptedException e) {
    55                 
    56                 e.printStackTrace();
    57             }
    58             synchronized (o1) {
    59                 
    60             }
    61         }
    62     }
    63 }

    2.利用线程同步机制交替输出奇数和偶数

     1 package javase.deadlock;
     2 
     3 public class ThreadTest2 {
     4     public static void main(String[] args) {
     5         Num num=new Num();
     6         Thread t1=new Thread(new MyThreadOne(num));
     7         Thread t2=new Thread(new MyThreadTwo(num));
     8         t1.setName("奇数");
     9         t2.setName("偶数");
    10         t1.start();
    11         t2.start();
    12     }
    13 }
    14 
    15 //创建数字类
    16 class Num{
    17     int i=1;//设定初始值为1
    18 }
    19 
    20 //输出奇数的线程
    21 class MyThreadOne implements Runnable{
    22     private Num num;
    23 
    24     public MyThreadOne(Num number) {
    25         this.num = number;
    26     }
    27 
    28     @Override
    29     public void run() {
    30         //一直输出奇数
    31         while (true){
    32             //上锁
    33             synchronized (num){
    34                 if(num.i%2==0){//若是偶数就wait
    35                     try {
    36                         num.wait();
    37                     } catch (InterruptedException e) {
    38                         e.printStackTrace();
    39                     }
    40                 }
    41                 //若不是偶数
    42                 System.out.println(Thread.currentThread().getName()+(num.i++));
    43                 //每隔一秒执行一次
    44                 try {
    45                     Thread.sleep(1000);
    46                 } catch (InterruptedException e) {
    47                     e.printStackTrace();
    48                 }
    49                 //唤醒
    50                 num.notify();
    51             }
    52         }
    53     }
    54 }
    55 
    56 //输出偶数的线程
    57 class MyThreadTwo implements Runnable{
    58     private Num num;
    59 
    60     public MyThreadTwo(Num number) {
    61         this.num = number;
    62     }
    63     @Override
    64     public void run() {
    65         while (true){
    66             synchronized (num){
    67                 if(num.i%2==1){//若是奇数就wait
    68                     try {
    69                         num.wait();
    70                     } catch (InterruptedException e) {
    71                         e.printStackTrace();
    72                     }
    73                 }
    74                 //若不是奇数
    75                 System.out.println(Thread.currentThread().getName()+(num.i++));
    76                 //每隔一秒执行一次
    77                 try {
    78                     Thread.sleep(1000);
    79                 } catch (InterruptedException e) {
    80                     e.printStackTrace();
    81                 }
    82                 //唤醒
    83                 num.notify();
    84             }
    85         }
    86     }
    87 }

    3..模拟这样一个需求:

    * 产库:list集合
    * list集合假设只能存一个元素
    * 1个元素就表示仓库满了
    * 如果list元素个数为0表示仓库空了
    * 保证list集合永远存一个元素

     1 package javase.deadlock;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 /**
     7  * 1.使用wait()方法和notify()方式实现生产者和消费者模式
     8  * 2."生产者消费者模式,一种特殊的业务需求"
     9  * 3.wait()和notify() 不是线程对象的方法,是普通JAVA对象都有的方法
    10  * 4.建立在线程同步基础上.要操作一个仓库,有线程安全问题
    11  * 5.wait()作用:o.wait()让o对象上的线程t进入等待状态,并且释放t之前占有的o对象的锁
    12  * 6.notify()作用:让o对象等待的线程唤醒,只是通知,不会释放o对象之前占有的对象的锁
    13  * 
    14  * 7.模拟这样一个需求:
    15  *    产库:list集合
    16  *    list集合假设只能存一个元素
    17  *    1个元素就表示仓库满了
    18  *     如果list元素个数为0表示仓库空了
    19  *     保证list集合永远存一个元素
    20  * @author yumu
    21  *
    22  */
    23 public class ThreadTest1 {
    24 @SuppressWarnings("rawtypes")
    25 public static void main(String[] args) {
    26     List list=new ArrayList();
    27     Thread t1=new  Thread(new Producer(list));
    28     Thread t2=new  Thread(new Consumer(list));
    29     t1.setName("生产者线程");
    30     t2.setName("消费者线程");
    31     t1.start();
    32     t2.start();
    33     
    34 }
    35 }
    36 //生产线程
    37 class Producer implements Runnable{
    38     private List list;
    39     
    40 
    41     public Producer(List list) {
    42         super();
    43         this.list = list;
    44     }
    45 
    46 
    47     @Override
    48     public void run() {
    49         synchronized (list) {
    50             while(true){        
    51             if(list.size()>0){
    52                 //说明集合有元素,进入等待
    53                 try {
    54                     list.wait();
    55                 } catch (InterruptedException e) {
    56                     
    57                     e.printStackTrace();
    58                 }
    59             }
    60             Object obj=new Object();
    61             list.add(obj);
    62             System.out.println(Thread.currentThread().getName()+"---------->"+obj);
    63             list.notifyAll();
    64         }
    65     }
    66     }
    67 }
    68 //消费线程
    69 class Consumer implements Runnable{
    70 private List list;
    71     
    72 
    73     public Consumer(List list) {
    74         super();
    75         this.list = list;
    76     }
    77     @Override
    78     public void run() {
    79         while(true){
    80         synchronized (list) {
    81             if(list.size()==0){
    82                 //说明集合没有元素,进入等待
    83                 try {
    84                     list.wait();
    85                 } catch (InterruptedException e) {
    86                     
    87                     e.printStackTrace();
    88                 }
    89             }
    90             //进行消费
    91             Object obj = list.remove(0);
    92             System.out.println(Thread.currentThread().getName()+"---------->"+obj);
    93             list.notifyAll();
    94         }
    95     }    
    96 }
    97 }

     4.写一个定时器:每隔十秒记录时间

     1 package javase.thread;
     2 
     3 import java.text.ParseException;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Date;
     6 import java.util.Timer;
     7 import java.util.TimerTask;
     8 
     9 /**
    10  * 定时器
    11  * @author yumu
    12  *
    13  */
    14 public class TimerTest {
    15 
    16     public static void main(String[] args) throws ParseException {
    17         //创建定时器对象
    18         Timer timer=new Timer();
    19         //Timer timer=new Timer(true);  //守护线程方式
    20         //指定定时任务
    21         //timer.schedule(定时任务, firstTime, 间隔时间);
    22         SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    23         Date firstTime = sdf.parse("2020-10-20 19:57:23");
    24         timer.schedule(new logTimerTask(), firstTime, 1000*10);
    25     
    26     }
    27 
    28 }
    29 //编写一个定时任务类
    30 //假设这是一个记录日志的定时任务
    31 class logTimerTask extends TimerTask{
    32 
    33     @Override
    34     public void run() {
    35         //编写需要执行的任务就行
    36         SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    37         String strTime = sdf.format(new Date());
    38         System.out.println(strTime+",完成了一次数据备份!");
    39     }
    40     
    41 }

     线程知识点:

     面试题:

  • 相关阅读:
    linux process management
    intel edison with grove lcd
    ODBC、OLEDB和ADO关系
    在线并使用数据库来推断在线
    大约laravel错误的解决方案
    cocos2dx-3.1加入cocosStudio参考库 libCocosStudio
    【甘道夫】基于Mahout0.9+CDH5.2执行分布式ItemCF推荐算法
    格式字符串分配stl::string
    希望开发一个指针的元素
    [Now] Configure secrets and environment variables with Zeit’s Now
  • 原文地址:https://www.cnblogs.com/yumu77/p/13868211.html
Copyright © 2011-2022 走看看