首页 文章

在main方法中启动手动线程

提问于
浏览
0

我在java中使用一些低级多线程,我有两种方法生成和使用:

public class Producer {

private LinkedList<Integer> list = new LinkedList();
private final int LIMIT = 10;
private Object lock = new Object();

public void produce() throws InterruptedException {

    int value = 0;

    while (true) {

        synchronized (lock) {

            // while loopet er til, for at blive ved med at tjekke at tjekke, at listen er fuld
            while (list.size() == LIMIT) {
                //notify vækker dette while-loop
                lock.wait(); //låsen venter indtil der er plads til at blive taget en ny værdi ud
                System.out.println("hej");
            }
            list.add(value++);
            lock.notify();
        }
    }
}

public void consume() throws InterruptedException {

    Random random = new Random();
    while (true) {
        synchronized (lock) {
            while (list.size() == 0) {
                lock.wait();
            }
            System.out.print("list size is " + list.size());
            int value = list.removeFirst();
            System.out.println("Current value is " + value);
            lock.notify();
        }

        Thread.sleep(random.nextInt(1000));

    }
  }
}

我可以在主要方法中放入什么线程来运行?既然我是在没有使用Thread of Runnable接口的情况下,我无法启动它们,并实例化一个对象,并且调用方法不起作用?

3 回答

  • 1

    您可以使用匿名线程执行此操作 .

    public static void main(String args[]) throws IOException, SaxonApiException {
        Producer producer = new Producer();
        new Thread()
        {
            public void run() {
                try {
                    producer.consume();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    
        new Thread()
        {
            public void run() {
    
                try {
                    producer.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    

    而我在输出中得到的是这个 .

    list size is 1Current value is 0
    list size is 10Current value is 1
    hej
    list size is 10Current value is 2
    hej
    list size is 10Current value is 3
    hej
    list size is 10Current value is 4
    hej
    
  • 1

    我假设这两种方法都在Producer类中 . 不需要其他课程 .

    public static void main(String... args) {
        Producer producer = new Producer();
        Thread t1 = new Thread(producer::produce);
        Thread t2 = new Thread(producer::consume);
        t1.start(); t2.start();
    }
    

    但首先必须从 produceconsume 方法的签名中删除 throws InterruptedException . 无论如何,从线程的根方法抛出异常都没有意义,因为没有调用者可以捕获并对该异常做出反应 . 只需捕获方法内的异常,打印stacktrace并返回 .

  • 0

    为了能够同时运行您的方法,您需要实现线程类/ runnable抽象的一些变体,如下所示:

    // Thread variant
    class MultithreadingObject extends Thread{
        public void run(){
            print("...");
        }
    } 
    
    public static void main(string[] args){
       threadOne = new MultithreadingObject();
       threadTwo = new MultithreadingObject();
       // Run both threads
       threadOne.start();
       threadTwo.start();
    }
    

    实现Runnable变体:

    public class MyThread extends Thread {
        public MyThread() {
            super("MyThread");
        }
        public void run() {
            //Code
        }
    }
    public static void main(string[] args){
      threadOne = new MyThread();
      threadTwo = new MyThread();
      // Run both threads
      threadOne.start();
      threadTwo.start();
    }
    

相关问题