java多线程 传递参数 启动50个线程,每个线程处理10条数据

createh52周前 (12-17)技术教程23

无参数的多线程

每个线程处理10条数据

package org.jeecg.modules.gybmapi;

public class ThreadTask implements Runnable{


public void run() {

for (int i = 0; i < 10; i++) {

System.out.println(Thread.currentThread().getName() + "执行" + i);

}

}


}

启动50个线程

package org.jeecg.modules.gybmapi;

public class Main {


public static void main(String[] args) {

for (int i = 0; i < 50; i++) {

new Thread(new ThreadTask()).start();

}

}

结果

Thread-0执行0

Thread-2执行0

Thread-2执行1

Thread-2执行2

Thread-1执行0

Thread-2执行3

Thread-1执行1

Thread-2执行4

Thread-1执行2

Thread-2执行5

Thread-1执行3

Thread-1执行4

Thread-1执行5

Thread-1执行6

Thread-1执行7

Thread-1执行8

Thread-1执行9

Thread-2执行6

Thread-2执行7

Thread-2执行8

Thread-2执行9

Thread-0执行1

Thread-0执行2

Thread-3执行0

Thread-3执行1

Thread-3执行2

Thread-3执行3

Thread-3执行4

Thread-3执行5

Thread-3执行6

Thread-3执行7

Thread-3执行8

Thread-3执行9

Thread-0执行3

Thread-0执行4

Thread-0执行5

Thread-0执行6

Thread-0执行7

Thread-0执行8

Thread-0执行9

Thread-4执行0

Thread-4执行1

Thread-4执行2

Thread-4执行3

Thread-4执行4

Thread-4执行5

Thread-4执行6

Thread-4执行7

Thread-4执行8

Thread-4执行9

Thread-5执行0

Thread-5执行1

Thread-5执行2

Thread-5执行3

Thread-5执行4

Thread-5执行5

Thread-5执行6

Thread-5执行7

Thread-5执行8

Thread-5执行9

Thread-6执行0

Thread-6执行1

Thread-6执行2

Thread-6执行3

Thread-6执行4

Thread-6执行5

Thread-6执行6

Thread-6执行7

Thread-6执行8

Thread-6执行9

Thread-7执行0

Thread-7执行1

Thread-7执行2

Thread-7执行3

Thread-7执行4

Thread-7执行5

Thread-7执行6

Thread-7执行7

Thread-7执行8

Thread-7执行9

Thread-8执行0

Thread-8执行1

Thread-8执行2

Thread-8执行3

Thread-8执行4

Thread-8执行5

Thread-8执行6

Thread-8执行7

Thread-8执行8

Thread-8执行9

Thread-9执行0

Thread-9执行1

Thread-9执行2

Thread-9执行3

Thread-9执行4

Thread-9执行5

Thread-9执行6

Thread-9执行7

Thread-9执行8

Thread-9执行9

Thread-10执行0

Thread-10执行1

Thread-10执行2

Thread-10执行3

Thread-10执行4

Thread-10执行5

Thread-10执行6

Thread-10执行7

Thread-10执行8

Thread-10执行9

Thread-11执行0

Thread-11执行1

Thread-11执行2

Thread-11执行3

Thread-11执行4

Thread-11执行5

Thread-11执行6

Thread-11执行7

Thread-11执行8

Thread-11执行9

Thread-12执行0

Thread-12执行1

Thread-12执行2

Thread-12执行3

Thread-12执行4

Thread-12执行5

Thread-12执行6

Thread-12执行7

Thread-12执行8

Thread-12执行9

Thread-13执行0

Thread-13执行1

Thread-13执行2

Thread-13执行3

Thread-13执行4

Thread-13执行5

Thread-13执行6

Thread-13执行7

Thread-13执行8

Thread-13执行9

Thread-14执行0

Thread-14执行1

Thread-14执行2

Thread-15执行0

Thread-15执行1

Thread-14执行3

Thread-15执行2

Thread-15执行3

Thread-15执行4

Thread-15执行5

Thread-14执行4

Thread-14执行5

Thread-14执行6

Thread-14执行7

Thread-14执行8

Thread-14执行9

Thread-15执行6

Thread-15执行7

Thread-15执行8

Thread-15执行9

Thread-16执行0

Thread-16执行1

Thread-16执行2

Thread-16执行3

Thread-16执行4

Thread-16执行5

Thread-16执行6

Thread-16执行7

Thread-16执行8

Thread-16执行9

Thread-17执行0

Thread-17执行1

Thread-17执行2

Thread-17执行3

Thread-17执行4

Thread-17执行5

Thread-17执行6

Thread-17执行7

Thread-17执行8

Thread-17执行9

Thread-18执行0

Thread-18执行1

Thread-18执行2

Thread-18执行3

Thread-18执行4

Thread-18执行5

Thread-18执行6

Thread-18执行7

Thread-18执行8

Thread-18执行9

Thread-19执行0

Thread-19执行1

Thread-19执行2

Thread-19执行3

Thread-19执行4

Thread-19执行5

Thread-19执行6

Thread-19执行7

Thread-19执行8

Thread-19执行9

Thread-20执行0

Thread-20执行1

Thread-20执行2

Thread-20执行3

Thread-20执行4

Thread-20执行5

Thread-20执行6

Thread-20执行7

Thread-20执行8

Thread-20执行9

Thread-21执行0

Thread-21执行1

Thread-21执行2

Thread-21执行3

Thread-21执行4

Thread-21执行5

Thread-21执行6

Thread-21执行7

Thread-21执行8

Thread-21执行9

Thread-23执行0

Thread-23执行1

Thread-23执行2

Thread-23执行3

Thread-23执行4

Thread-23执行5

Thread-23执行6

Thread-23执行7

Thread-23执行8

Thread-23执行9

Thread-22执行0

Thread-22执行1

Thread-22执行2

Thread-22执行3

Thread-22执行4

Thread-22执行5

Thread-22执行6

Thread-22执行7

Thread-22执行8

Thread-22执行9

Thread-24执行0

Thread-24执行1

Thread-24执行2

Thread-24执行3

Thread-24执行4

Thread-24执行5

Thread-24执行6

Thread-24执行7

Thread-24执行8

Thread-24执行9

Thread-25执行0

Thread-25执行1

Thread-25执行2

Thread-25执行3

Thread-25执行4

Thread-25执行5

Thread-25执行6

Thread-25执行7

Thread-25执行8

Thread-25执行9

Thread-26执行0

Thread-26执行1

Thread-26执行2

Thread-26执行3

Thread-26执行4

Thread-26执行5

Thread-26执行6

Thread-26执行7

Thread-26执行8

Thread-26执行9

Thread-27执行0

Thread-27执行1

Thread-27执行2

Thread-27执行3

Thread-27执行4

Thread-27执行5

Thread-27执行6

Thread-27执行7

Thread-27执行8

Thread-27执行9

Thread-28执行0

Thread-28执行1

Thread-28执行2

Thread-28执行3

Thread-28执行4

Thread-28执行5

Thread-28执行6

Thread-28执行7

Thread-28执行8

Thread-28执行9

Thread-29执行0

Thread-29执行1

Thread-29执行2

Thread-29执行3

Thread-29执行4

Thread-29执行5

Thread-29执行6

Thread-29执行7

Thread-29执行8

Thread-29执行9

Thread-30执行0

Thread-30执行1

Thread-30执行2

Thread-30执行3

Thread-30执行4

Thread-30执行5

Thread-30执行6

Thread-30执行7

Thread-30执行8

Thread-30执行9

Thread-31执行0

Thread-31执行1

Thread-31执行2

Thread-31执行3

Thread-31执行4

Thread-31执行5

Thread-31执行6

Thread-31执行7

Thread-31执行8

Thread-31执行9

Thread-32执行0

Thread-32执行1

Thread-32执行2

Thread-32执行3

Thread-32执行4

Thread-32执行5

Thread-32执行6

Thread-32执行7

Thread-32执行8

Thread-32执行9

Thread-33执行0

Thread-33执行1

Thread-33执行2

Thread-33执行3

Thread-33执行4

Thread-33执行5

Thread-33执行6

Thread-33执行7

Thread-33执行8

Thread-33执行9

Thread-34执行0

Thread-34执行1

Thread-34执行2

Thread-34执行3

Thread-34执行4

Thread-34执行5

Thread-34执行6

Thread-34执行7

Thread-34执行8

Thread-34执行9

Thread-35执行0

Thread-35执行1

Thread-35执行2

Thread-35执行3

Thread-35执行4

Thread-35执行5

Thread-35执行6

Thread-35执行7

Thread-35执行8

Thread-35执行9

Thread-36执行0

Thread-36执行1

Thread-36执行2

Thread-36执行3

Thread-36执行4

Thread-36执行5

Thread-36执行6

Thread-36执行7

Thread-36执行8

Thread-36执行9

Thread-37执行0

Thread-37执行1

Thread-37执行2

Thread-37执行3

Thread-37执行4

Thread-37执行5

Thread-37执行6

Thread-37执行7

Thread-37执行8

Thread-37执行9

Thread-38执行0

Thread-38执行1

Thread-38执行2

Thread-38执行3

Thread-38执行4

Thread-38执行5

Thread-38执行6

Thread-38执行7

Thread-38执行8

Thread-38执行9

Thread-48执行0

Thread-48执行1

Thread-48执行2

Thread-48执行3

Thread-48执行4

Thread-48执行5

Thread-48执行6

Thread-48执行7

Thread-48执行8

Thread-48执行9

Thread-47执行0

Thread-47执行1

Thread-47执行2

Thread-47执行3

Thread-47执行4

Thread-47执行5

Thread-47执行6

Thread-47执行7

Thread-47执行8

Thread-47执行9

Thread-46执行0

Thread-46执行1

Thread-46执行2

Thread-46执行3

Thread-46执行4

Thread-46执行5

Thread-46执行6

Thread-46执行7

Thread-46执行8

Thread-46执行9

Thread-49执行0

Thread-49执行1

Thread-49执行2

Thread-49执行3

Thread-49执行4

Thread-49执行5

Thread-49执行6

Thread-49执行7

Thread-49执行8

Thread-49执行9

Thread-43执行0

Thread-43执行1

Thread-43执行2

Thread-43执行3

Thread-43执行4

Thread-43执行5

Thread-43执行6

Thread-43执行7

Thread-43执行8

Thread-43执行9

Thread-41执行0

Thread-41执行1

Thread-41执行2

Thread-41执行3

Thread-41执行4

Thread-41执行5

Thread-41执行6

Thread-41执行7

Thread-41执行8

Thread-41执行9

Thread-44执行0

Thread-44执行1

Thread-44执行2

Thread-44执行3

Thread-44执行4

Thread-44执行5

Thread-44执行6

Thread-44执行7

Thread-44执行8

Thread-44执行9

Thread-40执行0

Thread-40执行1

Thread-40执行2

Thread-40执行3

Thread-40执行4

Thread-40执行5

Thread-40执行6

Thread-40执行7

Thread-40执行8

Thread-40执行9

Thread-39执行0

Thread-39执行1

Thread-39执行2

Thread-39执行3

Thread-39执行4

Thread-39执行5

Thread-39执行6

Thread-39执行7

Thread-39执行8

Thread-39执行9

Thread-45执行0

Thread-45执行1

Thread-45执行2

Thread-45执行3

Thread-45执行4

Thread-45执行5

Thread-45执行6

Thread-45执行7

Thread-45执行8

Thread-45执行9

Thread-42执行0

Thread-42执行1

Thread-42执行2

Thread-42执行3

Thread-42执行4

Thread-42执行5

Thread-42执行6

Thread-42执行7

Thread-42执行8

Thread-42执行9

Process finished with exit code 0

2 带参数的多线程

启动2个线程,每个线程处理100条数据

2.1构造方法传参

package org.jeecg.modules.gybmapi;

public class Printer implements Runnable {

private String message;

public Printer(String message) {

this.message = message;

}

@Override

public void run() {

for (int i = 0; i < 100; i++) {

System.out.println(Thread.currentThread().getName() + "执行" + i +message);

}

}

}

2.2启动2个线程

package org.jeecg.modules.gybmapi;

public class Main {


public static void main(String[] args) {

//启动2 个线程

new Thread(new Printer("Good")).start();

new Thread(new Printer("Nice")).start();

}

}

2.3 set方法传参

package org.jeecg.modules.gybmapi;

public class Printer implements Runnable {

private String message;

public void setMessage(String message) {

this.message = message;

}

// public Printer(String message) {

// this.message = message;

// }

@Override

public void run() {

for (int i = 0; i < 100; i++) {

System.out.println(Thread.currentThread().getName() + "执行" + i +message);

}

}

}

2.4 启动2个线程

package org.jeecg.modules.gybmapi;

public class Main {


public static void main(String[] args) {

// for (int i = 0; i < 50; i++) {

// new Thread(new ThreadTask()).start();

// }

//启动2 个线程

Printer printer=new Printer();

printer.setMessage("Good");

Printer printer1=new Printer();

printer1.setMessage("Nice");

new Thread(printer).start();

new Thread(printer1).start();

}

}

2.5结果

D:\software\java8\jdk8\bin\java.exe

Thread-0执行0Good

Thread-0执行1Good

Thread-0执行2Good

Thread-0执行3Good

Thread-0执行4Good

Thread-0执行5Good

Thread-0执行6Good

Thread-0执行7Good

Thread-0执行8Good

Thread-0执行9Good

Thread-0执行10Good

Thread-0执行11Good

Thread-0执行12Good

Thread-0执行13Good

Thread-0执行14Good

Thread-0执行15Good

Thread-0执行16Good

Thread-0执行17Good

Thread-1执行0Nice

Thread-1执行1Nice

Thread-1执行2Nice

Thread-1执行3Nice

Thread-1执行4Nice

Thread-1执行5Nice

Thread-1执行6Nice

Thread-1执行7Nice

Thread-1执行8Nice

Thread-1执行9Nice

Thread-1执行10Nice

Thread-1执行11Nice

Thread-1执行12Nice

Thread-1执行13Nice

Thread-1执行14Nice

Thread-1执行15Nice

Thread-1执行16Nice

Thread-1执行17Nice

Thread-1执行18Nice

Thread-1执行19Nice

Thread-1执行20Nice

Thread-1执行21Nice

Thread-1执行22Nice

Thread-1执行23Nice

Thread-1执行24Nice

Thread-1执行25Nice

Thread-1执行26Nice

Thread-1执行27Nice

Thread-1执行28Nice

Thread-1执行29Nice

Thread-1执行30Nice

Thread-1执行31Nice

Thread-1执行32Nice

Thread-1执行33Nice

Thread-1执行34Nice

Thread-1执行35Nice

Thread-1执行36Nice

Thread-1执行37Nice

Thread-1执行38Nice

Thread-1执行39Nice

Thread-1执行40Nice

Thread-1执行41Nice

Thread-1执行42Nice

Thread-1执行43Nice

Thread-1执行44Nice

Thread-1执行45Nice

Thread-1执行46Nice

Thread-1执行47Nice

Thread-1执行48Nice

Thread-1执行49Nice

Thread-1执行50Nice

Thread-1执行51Nice

Thread-1执行52Nice

Thread-1执行53Nice

Thread-1执行54Nice

Thread-1执行55Nice

Thread-1执行56Nice

Thread-1执行57Nice

Thread-1执行58Nice

Thread-1执行59Nice

Thread-1执行60Nice

Thread-1执行61Nice

Thread-1执行62Nice

Thread-1执行63Nice

Thread-1执行64Nice

Thread-1执行65Nice

Thread-1执行66Nice

Thread-1执行67Nice

Thread-1执行68Nice

Thread-1执行69Nice

Thread-1执行70Nice

Thread-1执行71Nice

Thread-1执行72Nice

Thread-1执行73Nice

Thread-1执行74Nice

Thread-1执行75Nice

Thread-1执行76Nice

Thread-1执行77Nice

Thread-1执行78Nice

Thread-1执行79Nice

Thread-1执行80Nice

Thread-1执行81Nice

Thread-1执行82Nice

Thread-1执行83Nice

Thread-1执行84Nice

Thread-1执行85Nice

Thread-1执行86Nice

Thread-1执行87Nice

Thread-1执行88Nice

Thread-1执行89Nice

Thread-1执行90Nice

Thread-1执行91Nice

Thread-1执行92Nice

Thread-1执行93Nice

Thread-1执行94Nice

Thread-1执行95Nice

Thread-1执行96Nice

Thread-1执行97Nice

Thread-1执行98Nice

Thread-1执行99Nice

Thread-0执行18Good

Thread-0执行19Good

Thread-0执行20Good

Thread-0执行21Good

Thread-0执行22Good

Thread-0执行23Good

Thread-0执行24Good

Thread-0执行25Good

Thread-0执行26Good

Thread-0执行27Good

Thread-0执行28Good

Thread-0执行29Good

Thread-0执行30Good

Thread-0执行31Good

Thread-0执行32Good

Thread-0执行33Good

Thread-0执行34Good

Thread-0执行35Good

Thread-0执行36Good

Thread-0执行37Good

Thread-0执行38Good

Thread-0执行39Good

Thread-0执行40Good

Thread-0执行41Good

Thread-0执行42Good

Thread-0执行43Good

Thread-0执行44Good

Thread-0执行45Good

Thread-0执行46Good

Thread-0执行47Good

Thread-0执行48Good

Thread-0执行49Good

Thread-0执行50Good

Thread-0执行51Good

Thread-0执行52Good

Thread-0执行53Good

Thread-0执行54Good

Thread-0执行55Good

Thread-0执行56Good

Thread-0执行57Good

Thread-0执行58Good

Thread-0执行59Good

Thread-0执行60Good

Thread-0执行61Good

Thread-0执行62Good

Thread-0执行63Good

Thread-0执行64Good

Thread-0执行65Good

Thread-0执行66Good

Thread-0执行67Good

Thread-0执行68Good

Thread-0执行69Good

Thread-0执行70Good

Thread-0执行71Good

Thread-0执行72Good

Thread-0执行73Good

Thread-0执行74Good

Thread-0执行75Good

Thread-0执行76Good

Thread-0执行77Good

Thread-0执行78Good

Thread-0执行79Good

Thread-0执行80Good

Thread-0执行81Good

Thread-0执行82Good

Thread-0执行83Good

Thread-0执行84Good

Thread-0执行85Good

Thread-0执行86Good

Thread-0执行87Good

Thread-0执行88Good

Thread-0执行89Good

Thread-0执行90Good

Thread-0执行91Good

Thread-0执行92Good

Thread-0执行93Good

Thread-0执行94Good

Thread-0执行95Good

Thread-0执行96Good

Thread-0执行97Good

Thread-0执行98Good

Thread-0执行99Good

Process finished with exit code 0

相关文章

java -jar 启动参数 java -jar 启动参数 内存

/usr/local/java/jdk1.8.0_131/bin/java -jar -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=...

腾讯大佬详细讲解Java 启动exe程序,传递参数和获取参数

这篇文章主要介绍了java 启动exe程序,传递参数和获取参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧1、java中启动exe程序 ,并添加传参String[] cmd =...

JVM诊断之查看运行参数 jvm运行参数说明

问题描述为了分析和定位一个Java线上系统问题,我们需要查看JVM启动时的一些参数设置,例如:垃圾回收算法、堆大小等等。这些参数可能在启动脚本中明确指明,也可能采用默认值。在系统运行过程中其他人也许动...

Java方法参数传递的机制 java方法参数可以传空吗

目前两种比较流行的方法参数传递模式主要是值传递和引用传递。不同的编程语言对于这两种机制可能有不同的处理方式。对Java来说,一切都是严格按值传递的。接下来,我们来一起探讨下 Java 如何为各种类型传...

java面试题|JVM调优常用JVM参数代码实例简介

JVM(Java Virtual Machine)启动参数用于配置Java应用程序的运行时环境,包括内存管理、垃圾回收(GC)算法、性能调优和日志记录等。# JVM启动参数,不换行格式 # 设置堆内...

Tomcat、JVM 参数如何调到性能最好?

Tomcat性能调优找到Tomcat根目录下的conf目录,修改server.xml文件的内容。对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创建的...