java编写Producer(线程池,kafka)

  • Post author:
  • Post category:java


1.将kafka带的jar包导入项目内

2

​public class TestThreadPool {
    public static void main(String args[]) {
        //在线程池中创建2个线程
        ExecutorService exec = Executors.newFixedThreadPool(2);
        //创建100个线程目标对象
        for (int index = 0; index < 100; index++) {
            Runnable run = new Runner(index);
            //执行线程目标对象
            exec.execute(run);
        }
        exec.shutdown();
    }
}

//线程目标对象
class Runner implements Runnable {
    int index = 0;
    public static String topic = "test";

    public Runner(int index) {
        this.index = index;
    }

    public void run() {
        long time = (long) (Math.random() * 1000);
        Producer producer = createProducer();
        KeyedMessage<String, String> keyedMessage = new KeyedMessage<String, String>(topic, "线程:" + Thread.currentThread().getName() + "(目标对象" 



版权声明:本文为qq_27020369原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。