设计两个线程,对一个初始值为1的整型变量进行操作,一个线程对变量i进行自加操作,另一个线程对变量i进行自减的操作,要求在程序中变量的值符合010101010。的规则变化java线程面试题设计

解决方案 »

  1.   

    考的是线程同步。使用wait、notify进行同步。
    其实这个有点类似于生产者、消费者,不过在这里生产者和消费者都只有一个,而且缓冲区的长度也只有1。
      

  2.   

    我的理解是这样的,由于本机器上没有安装java环境,只能这样设想。
    1)线程同步;
    2)增量时,若已经为1,则使自己睡眠,并释放同步锁;
    3)减量时,若已经为0,则使自己睡眠,并释放同步锁。
      

  3.   

    好吧因为之前说了这句话,所以不得不写一个并非完美的解法其实判断还是要的,只是不会每一次被唤醒都要做判断
    package test;public class Test implements Runnable {
      private static int iValue = 0;  private static Object iLock = new Object();  private final int iStepSize;  public Test(final int stepSize) {
        iStepSize = stepSize;
      }  @Override
      public void run() {
        while (true) {
          System.out.print(iValue);
          iValue += iStepSize;
          synchronized (iLock) {
            iLock.notifyAll();
            try {
              iLock.wait();
            }
            catch (InterruptedException e) {        }
          }
        }
      }  public static void main(final String[] args) throws Exception {
        Thread t1 = new Thread(new Test(1));
        Thread t2 = new Thread(new Test(-1));
        t1.start();
        Thread.sleep(100);
        t2.start();
        Thread.sleep(500);
      }
    }
      

  4.   

    不多说,直接上代码:
    public class Test {
    int i = 1; public synchronized void calcNumber() {
    if (i != 0) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    i++;
    System.out.print(i);
    notify();
    } public synchronized void calcNumber2() {
    if (i != 1) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    i--;
    System.out.print(i);
    notify();
    }
    }
    public class Thread1 implements Runnable {
    Test t; public Thread1(Test t) {
    this.t = t;
    } @Override
    public void run() {
    for (int i = 0; i < 10; i++) {
    t.calcNumber();
    }
    }}
    public class Thread2 implements Runnable {
    Test t; public Thread2(Test t) {
    this.t = t;
    } @Override
    public void run() {
    for (int i = 0; i < 10; i++) {
    t.calcNumber2();
    }
    }}
    /**
     * 测试类
     * @author Administrator
     *
     */
    public class Test2 { /**
     * @param args
     */
    public static void main(String[] args) {
    Test t = new Test();
    new Thread(new Thread2(t)).start();
    new Thread(new Thread1(t)).start();
    }}
      

  5.   

    下面这种写法怎么改?
    public class thread {
    private int j=0;  
        public static void main(String[] args) {  
         thread incDec=new thread();  
            Inc inc=incDec.new Inc();  
            Dec dec=incDec.new Dec();  
            while(true){  
                Thread thread=new Thread(inc);  
                thread.start();  
                thread=new Thread(dec);  
                thread.start();  
            }  
        }  
      
        public synchronized void inc(){  
            j++;  
            System.out.println(Thread.currentThread().getName()+"-inc:"+j);  
        }  
        public synchronized void dec(){  
            j--;  
            System.out.println(Thread.currentThread().getName()+"-dec:"+j);  
        }  
          
        class Inc implements Runnable{  
            public void run(){  
                    inc();  
            }  
        }  
        class Dec implements Runnable{  
            public void run(){  
                    dec();  
            }  
        } 
    }
      

  6.   

    什么意思,我觉得改成这样可以达到楼主想要的效果
    public class thread {
    private int j=0;
    private static Object iLock = new Object();
        public static void main(String[] args) {  
         thread incDec=new thread();  
            Inc inc=incDec.new Inc();  
            Dec dec=incDec.new Dec();  
            while(true){  
                Thread thread=new Thread(inc);  
                thread.start();  
                thread=new Thread(dec);  
                thread.start();  
            }  
        }  
      
        public synchronized void inc(){  
            j++;  
            System.out.println(Thread.currentThread().getName()+"-inc:"+j);  
        }  
        public synchronized void dec(){  
            j--;  
            System.out.println(Thread.currentThread().getName()+"-dec:"+j);  
        }  
          
        class Inc implements Runnable{  
            public void run(){  
             synchronized (iLock) {
             if(j==0){
             j++;  
                    System.out.println(Thread.currentThread().getName()+"-inc:"+j);  
                 iLock.notifyAll();
                 try {
                   iLock.wait();
                 }
                 catch (InterruptedException e) {
          
                 }
                 }
               }
            }  
        }  
        class Dec implements Runnable{  
            public void run(){  
             synchronized (iLock) {
             if(j==1){
             j--;  
                    System.out.println(Thread.currentThread().getName()+"-dec:"+j);  
                 iLock.notifyAll();
                 try {
                   iLock.wait();
                 }
                 catch (InterruptedException e) {
          
                 }
             }
               } 
            }  
        } 
    }
      

  7.   


    public class thread {
    private int j=0;
    private static Object iLock = new Object();
        public static void main(String[] args) {  
         thread incDec=new thread();  
            Inc inc=incDec.new Inc();  
            Dec dec=incDec.new Dec();  
            while(true){  
                Thread thread=new Thread(inc);  
                thread.start();  
                thread=new Thread(dec);  
                thread.start();  
            }  
        }  
        class Inc implements Runnable{  
            public void run(){  
             synchronized (iLock) {
             if(j==0){
             j++;  
                    System.out.println(Thread.currentThread().getName()+"-inc:"+j);  
                 iLock.notifyAll();
                 try {
                   iLock.wait();
                 }
                 catch (InterruptedException e) {
          
                 }
                 }
               }
            }  
        }  
        class Dec implements Runnable{  
            public void run(){  
             synchronized (iLock) {
             if(j==1){
             j--;  
                    System.out.println(Thread.currentThread().getName()+"-dec:"+j);  
                 iLock.notifyAll();
                 try {
                   iLock.wait();
                 }
                 catch (InterruptedException e) {
          
                 }
             }
               } 
            }  
        } 
    }
    输出结果:.............
    Thread-17639-dec:0
    Thread-17640-inc:1
    Thread-17647-dec:0
    Thread-17646-inc:1
    Thread-17649-dec:0
    Thread-17650-inc:1
    Thread-17653-dec:0
    Thread-17654-inc:1
    Thread-17655-dec:0
    Thread-17656-inc:1
    Thread-17657-dec:0
    Thread-17662-inc:1
      

  8.   


    package com.netviewtech.iot.dapi.client.impl;
    public class Test { volatile int i = 1 ; public void start(){ final Object lock = new Object() ; // 自增
    new Thread(){
    public void run() {
    for(int j = 0 ; j < 1000 ; j++){
    synchronized (lock) {
    try {
    lock.wait() ;
    } catch (InterruptedException e) {
    e.printStackTrace();
    }  
    i++ ;
    System.out.print(i);
    lock.notify() ;
    }
    }
    };
    }.start(); 
    // 自减
    new Thread(){
    public void run() {
    for(int j = 0 ; j < 1000 ; j++){
    synchronized (lock) {
    i-- ;
    System.out.print(i);
    lock.notify() ;
    try {
    lock.wait() ;
    } catch (InterruptedException e) {
    e.printStackTrace();

    }
    }
    };
    }.start();  } public static void main(String[] args) { new Test().start() ; }
    }
      

  9.   

    你应该这样记忆:
    1,先有共享资源(就是一个类,属性就是资源,一般有add和delete方法对资源进行操作)。
    2,多线程(在这里就是两个线程consumer和producer,实现runnable接口,复写run方法时要进行加锁操作)。
    3,主线程(创建线程,并start)。总:就是三个类而已
      

  10.   

    我也刚看到现成,也写了下,结果是对了,不知道代码里面是不是有待提高
    public class Test
    {
    public static void main(String[] args) throws Exception{
    int i = 1;
    Thread t1 = new Thread(new processor(new User(i)));
    Thread t2 = new Thread(new processor(new User(i)));
    t1.setName("t1");
    t2.setName("t2"); t1.start();
    Thread.sleep(100);
    t2.start();

    }
    }class processor implements Runnable
    {
    User u;
    processor(User u){
    this.u =u;
    }
    public void run(){
    if(Thread.currentThread().getName().equals("t1")){
    while(true){
    try{User.m1();
    System.out.println(Thread.currentThread().getName()+"-->"+u.getU());
    Thread.sleep(1000);
    }catch(Exception r){}

    }
    }
    if(Thread.currentThread().getName().equals("t2")){
    while(true){
    try{User.m2();
    System.out.println(Thread.currentThread().getName()+"-->"+u.getU());
    Thread.sleep(1000);
    }catch(Exception r){}

    }
    }

    }
      
    }class User
    { private static int i;
    User(int i){
    this.i = i;
    }
    public static void setU(int i){
    User.i = i;;
    }
    public static int getU(){
    return i;
    }
    public synchronized static void m1(){
    int a = User.i - 1;
    User.setU(a);
    }
    public synchronized static void m2(){
    int a = User.i + 1;
    User.setU(a);
    }}
      

  11.   

    刚才看到,我把Exception e 写成 Exception r了