public class Thread extends Object implements Runnable
public class FirstThread extends Thread {
public void run(){
for(int i=0;i<100;i++){
/*
* Thread类已经继承了Object
* Object类创建了name选项 并且有其getName(),setName()方法
* 在继承Thread的类里面使用时只需要用this引用
*/
System.out.println(this.getName()+" "+i);
}
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
if(i==20){
new FirstThread().start();
new FirstThread().start();
}
}
}
}
public Thread() public Thread(Runnable target) public Thread(Runnable target,String name)
public class SecondThread implements Runnable {
public void run(){
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
if(i==20){
SecondThread st=new SecondThread();
//通过new Thread(target,name)创建线程
new Thread(st,"新线程1").start();
new Thread(st,"新线程2").start();
}
}
}
}
class Thread1 extends Thread{
private int count=5;
private String name;
public Thread1(String name) {
this.name=name;
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(name + "运行 count= " + count--);
try {
sleep((int) Math.random() * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread1 mTh1=new Thread1("A");
Thread1 mTh2=new Thread1("B");
mTh1.start();
mTh2.start();
}
}
B运行 count= 5 A运行 count= 5 B运行 count= 4 B运行 count= 3 B运行 count= 2 B运行 count= 1 A运行 count= 4 A运行 count= 3 A运行 count= 2 A运行 count= 1
class Thread2 implements Runnable{
private int count=15;
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "运行 count= " + count--);
try {
Thread.sleep((int) Math.random() * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Thread2 my = new Thread2();
new Thread(my, "C").start();//同一个mt,但是在Thread中就不可以,如果用同一个实例化对象mt,就会出现异常
new Thread(my, "D").start();
new Thread(my, "E").start();
}
}
C运行 count= 15 D运行 count= 14 E运行 count= 13 D运行 count= 12 D运行 count= 10 D运行 count= 9 D运行 count= 8 C运行 count= 11 E运行 count= 12 C运行 count= 7 E运行 count= 6 C运行 count= 5 E运行 count= 4 C运行 count= 3 E运行 count= 2
public class ThirdThread implements Callable<Integer> {
public Integer call(){
int i=0;
for(;i<100;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
}
return i;
}
public static void main(String[] args){
ThirdThread tt=new ThirdThread();
FutureTask<Integer> task=new FutureTask<>(tt);
Thread t=new Thread(task,"有返回值的线程");
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+" "+i);
if(i==20){
t.start();
}
}
try{
System.out.println("返回值是:"+task.get());
}catch(Exception e){
e.printStackTrace();
}
}
}
public class ThirdThread{
public static void main(String[] args){
ThirdThread tt=new ThirdThread();
//先使用Lambda表达式创建Callable<Integer>对象
//使用FutureTask封装Callable对象
FutureTask<Integer> task=new FutureTask<Integer>((Callable<Integer>)()->{
int i=0;
for(;i<100;i++){
System.out.println(Thread.currentThread().getName()+"的循环变量i的值:"+i);
}
return i;
});
for(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+"的循环变量i的值:"+i);
if(i==20){
new Thread(task,"有返回值的线程").start();
}
}
try{
System.out.println("子线程的返回值"+task.get());
}catch(Exception e){
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有