Java 在main方法中启动手动线程

Java 在main方法中启动手动线程,java,multithreading,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

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

    }
  }
}
公共类制作人{
私有LinkedList=新建LinkedList();
私人最终整数限制=10;
私有对象锁=新对象();
public void product()引发InterruptedException{
int值=0;
while(true){
已同步(锁定){
//而活套直到,因为在布利夫,我在tjekke,在tjekke,在倾听
while(list.size()=限制){
//循环时通知vækker dette
lock.wait();//låsen venter indtil der plads直到在blive taget en ny værdi ud
System.out.println(“hej”);
}
添加(value++);
lock.notify();
}
}
}
public void consume()引发InterruptedException{
随机=新随机();
while(true){
已同步(锁定){
while(list.size()==0){
lock.wait();
}
系统输出打印(“列表大小为”+列表大小();
int value=list.removeFirst();
System.out.println(“当前值为”+值);
lock.notify();
}
Thread.sleep(random.nextInt(1000));
}
}
}

我可以在main方法中放入什么来运行线程?由于我没有使用Runnable接口的线程,我无法启动它们,实例化对象和调用方法不起作用?

为了能够同时运行您的方法,您需要实现Thread类/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();
}
实现可运行的变量:

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

为了能够同时运行方法,您需要实现thread类/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();
}
实现可运行的变量:

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

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

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

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

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

我假设这两种方法都是类生产者。不需要其他课程

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

但是首先必须从
生成
消费
方法的签名中删除
抛出中断异常。不管怎样,从线程的根方法抛出异常是没有意义的,因为没有调用方可以捕获并响应该异常。只需捕获方法内部的异常,打印stacktrace并返回。

我假设这两个方法都在类生成器中。不需要其他课程

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

但是首先必须从
生成
消费
方法的签名中删除
抛出中断异常。不管怎样,从线程的根方法抛出异常是没有意义的,因为没有调用方可以捕获并响应该异常。只需捕获方法内部的异常,打印stacktrace并返回。

您是否有不使用的原因,例如
Runnable
?要添加关于使用Runnable而不是扩展线程的其他注释:@MickMnemonic只是练习更好地理解多线程的基本原理,不适用于实际应用程序您是否有理由不使用例如
Runnable
?在关于使用Runnable而不是扩展线程的其他注释中添加:@MickMnemonic只是练习更好地理解多线程的基本原理,而不是适用于实际应用程序