在主要方法中启动手动线程

时间:2018-08-27 20:17:20

标签: java multithreading

我正在使用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));

    }
  }
}

我可以在线程运行的主要方法中放入什么?由于我不是在使用Runnable接口的Thread,所以无法启动它们,实例化对象并调用方法不起作用?

3 个答案:

答案 0 :(得分: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 :(得分: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();
}

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

答案 2 :(得分:0)

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

// 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();
}

可运行的实现变体:

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();
}