zoukankan      html  css  js  c++  java
  • 编写两个线程,顺序输出自然顺序:1,2,3,4,...,99,100

    并发编程。请编写两个线程,线程1顺序输出1,3,5,...,99等奇数,每个数一行。线程2顺序输出2,4,6,...,100等偶数,每个数一行。最终的结果要求是输出为自然顺序:1,2,3,4,...,99,100。

     方法一:

    创建线程类RunnableDemo1.Java

    public class RunnableDemo1 implements Runnable{
    
        private Thread t;
        private String threadName;
        private Object curlock;
    
        RunnableDemo1( String name,Object lock) {
            super();
            threadName = name;
            curlock = lock;
        }
    
        public void run() {
            synchronized (curlock) {
                try {
                    for (int i = 1; i < 100; ) {
                        System.out.println(i);
                        i = i + 2;
                        curlock.notifyAll();
                        curlock.wait();
                    }
                } catch (InterruptedException e) {
                    System.out.println("Thread " + threadName + " interrupted.");
                }
            }
        }
        public void start () {
    //        System.out.println("Starting " +  threadName );
            if (t == null) {
                t = new Thread (this, threadName);
                t.start ();
            }
        }
    
        public void interrupt () {
            if (t == null) {
                t = new Thread (this, threadName);
                t.interrupt ();
            }
        }
    }
    

      创建线程类RunnableDemo2.Java

    public class RunnableDemo2 implements Runnable{
    
        private Thread t;
        private String threadName;
        private Object curlock ;
    
        RunnableDemo2( String name,Object lock) {
            super();
            threadName = name;
            curlock = lock;
        }
    
        public void run() {
            synchronized (curlock) {
                try {
                    for(int i = 2; i <= 100; ) {
                        System.out.println(i);
                        i=i+2;
                        curlock.notifyAll();
                        curlock.wait();
                    }
                }catch (InterruptedException e) {
                    System.out.println("Thread " +  threadName + " interrupted.");
                }
            }
        }
    
        public void start () {
            if (t == null) {
                t = new Thread (this, threadName);
                t.start ();
            }
        }
    
        public void interrupt () {
            if (t == null) {
                t = new Thread (this, threadName);
                t.interrupt ();
            }
        }
    }
    

      测试类代码:

    public class Test {
    
        public static void main(String args[]) throws Exception{
            final Object lock = new Object();
            RunnableDemo1 R1 = new RunnableDemo1( "Thread-1",lock);
            R1.start();
            Thread.sleep(100);
            RunnableDemo2 R2 = new RunnableDemo2( "Thread-2",lock);
            R2.start();
            R1.interrupt();
            R2.interrupt();
        }
    }
    

     方法二:

    public class ThreadTest implements Runnable{
        int i = 1;
        public void run() {
            while (true) {
                /*ThreadTest,因为使用的是implements方式。若使用继承Thread类的方式,慎用this*/
                synchronized (this) {
                    /*唤醒另外一个线程,注意是this的方法,而不是Thread*/
                    notify();
                    try {
                        /*使其休眠100毫秒,放大线程差异*/
                        Thread.currentThread();
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (i <= 100) {
                        System.out.println(i);
                        i++;
                        try {
                            /*放弃资源,等待*/
                            wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    

      测试方法如下:

    public static void main(String[] args) {
    
            ThreadTest t = new ThreadTest();
            Thread t1 = new Thread(t);
            Thread t2 = new Thread(t);
            t1.start();
            t2.start();
    
        }
    

      

  • 相关阅读:
    linux目录文件与系统启动(1)系统目录简介
    linux常用命令
    linux命令行
    关于Xshell6无法连接虚拟机centOS7的解决方案
    远程连接服务器和查错
    ip、端口、协议、网关、DNS
    linux分区
    linux系统补丁更新 yum命令
    VMWare网络适配器的三种模式
    springboot 配置springmvc?
  • 原文地址:https://www.cnblogs.com/loytime/p/11714219.html
Copyright © 2011-2022 走看看