java多线程

//1.//1.创建Runnable接口实现多线程

public class Main {

	static int[] next = new int[1000001];
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Runnable_test r1 = new Runnable_test("Thread-1");
		r1.start();
		
		Runnable_test r2 = new Runnable_test("Thread-2");
		r2.start();                
    }  	
}

class Runnable_test implements Runnable
{

	Thread t;
	String str;
	
	public Runnable_test(String str) {
		this.str = str;
		System.out.println("Creating " + this.str);
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Running " + str);
		for(int i = 0;i<4;i++)
		{
			System.out.println("Thread: " + str+" "+i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("Thread :"+str+" interrupted");
			}
		}
		System.out.println("Thread: "+str+" exiting");
	}
	public void start()
	{
		System.out.println("Starting "+str);
		if(t == null)
		{
			t = new Thread(this, str);
			t.start();
		}
	}
	
}

//2.继承thread实现多线程

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Thread_test t1 = new Thread_test("Thread-1");
		t1.start();
		
		Thread_test t2 = new Thread_test("Thread-2");
		t2.start();
	}

}

class Thread_test extends Thread
{
	Thread t;
	String str;
	Thread_test(String str)
	{
		this.str = str;
		System.out.println("Creating " + this.str);
	}
	
	public void run()
	{
		System.out.println("Running "+str);
		for(int i = 0;i<4;i++)
		{
			System.out.println("Thread: "+str+" "+i);
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				System.out.println("Thread :"+str+" interrupted");
			}
		}
		System.out.println("Thread: "+str+" exiting");
	}
	
	public void start()
	{
		System.out.println("Starting "+str);
		if(t == null)
		{
			t = new Thread(this, str);
			t.start();
		}
	}
}

//3.通过Callable和Future创建进程

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableThread implements Callable<Integer>{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CallableThread ct = new CallableThread();
		FutureTask<Integer> ft = new FutureTask<>(ct);
		
		for(int i = 0;i<100;i++)
		{
			System.out.println(Thread.currentThread().getName()+" "+i);
			if(i == 20)
			{
				new Thread(ft, "returned thread").start();
			}
		}
		try {
			System.out.println("son's thrread returned value: "+ft.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Integer call() throws Exception {
		// TODO Auto-generated method stub
		//return null;
		int i = 0;
		for(;i<100;i++)
		{
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
		return i;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值