Java 线程

创建线程(extends Thread和implements Runnable):

extends Thread

/**
 * 继承Thread类,自定义线程
 * @author 30869
 *
 */
public class MyThread extends Thread{
	public void run(){
		for(int i=0;i<=100;i++){
			System.out.println(MyThread.currentThread().getName()+":"+i);
		}
	}
}

/**
 * 测试MyThread,启动线程
 * 
 * @author 30869
 *
 */
public class TestMyThread {

	public static void main(String[] args) {
		MyThread myThread1 = new MyThread();//创建对象
		MyThread myThread2 = new MyThread();
		myThread1.start();//启动线程1
		myThread2.start();//启动线程2(自动线程)
//		myThread1.run();//属于调用方法,不存在自动线程,单线程,顺序执行
//		myThread2.run();	
	}

}


implements Runnable
/**
 * 测试Runnable接口
 * @author 30869
 *
 */
public class MyRunnable implements Runnable {//实现Runnable接口

	@Override
	public void run() {//实现run()
		for(int i=0;i<=100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}
/**
 * 测试MyRunnable
 * @author 30869
 *
 */
public class TestMyRunnable {

	public static void main(String[] args) {
		//将实现了Runnable接口run()的对象作为参数传入Thread,创建Thread对象
		Thread thread=new Thread(new MyRunnable());
		thread.start();//启动线程
	}

}


线程常用方法:

获取线程引用

/**
 * 测试Thread类的静态方法currentThread(),该方法返回一个调用它的线程的引用
 * @author 30869
 *
 */
public class TestThread_currentThread {

	public static void main(String[] args) {
		Thread t=new Thread();
		t=Thread.currentThread();//获取当前线程引用
		System.out.println(t.getName());//输出线程名
		t.setName("我的第一个线程");//设置此线程名
		System.out.println(t.getName());
	}

}


休眠

/**
 * 测试线程休眠
 * @author 30869
 *
 */
public class TestThread_sleep {

	public static void main(String[] args) {
		System.out.println("wait");
		Mysleep(10);
		System.out.println("start");
	}

	public static void Mysleep(int s) {
		for (int i = 1; i <= s; i++) {
			System.out.println(i+"秒");
			try {
				Thread.sleep(1000);//休眠1秒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}


优先级

public class MyThread implements Runnable {

	@Override
	public void run() {
		for(int i=0;i<=100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}


/**
 * 测试线程的优先级priority
 * @author 30869
 *
 */
public class TestThread_priority {

	public static void main(String[] args) {
		Thread thread1=new Thread(new MyThread(),"高优先级");//创建线程的同时取名字
		Thread thread2=new Thread(new MyThread(),"低优先级");
		thread1.setPriority(Thread.MAX_PRIORITY);//设置优先级最高
		thread2.setPriority(Thread.MIN_PRIORITY);//设置优先级最低
		thread1.start();
		thread2.start();
	}

}

线程阻塞

public class MyThread implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(1000);
				System.out.println(Thread.currentThread().getName()+":"+i);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
/**
 * 测试阻塞线程
 * @author 30869
 *
 */
public class TestThread_join {

	public static void main(String[] args) {
		Thread thread=new Thread(new MyThread());
		thread.start();
		for(int i=0;i<=20;i++){
			if(i==1){
				try {
					thread.join();//阻塞主线程,强制执行调用join()的子线程,直到子线程执行完毕,再解除阻塞状态
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

}

线程礼让

public class MyThread implements Runnable {

	@Override
	public void run() {
		for(int i=0;i<5;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
			if(i==3){
				System.out.print("线程礼让");
				Thread.yield();//当前线程转为就绪,与其他相同或更高优先级的线程争夺运行权利,若没有相同或更高优先级,继续执行此线程
			}
		}
	}

}
public class TestThread_yield {

	public static void main(String[] args) {
		MyThread myThread=new MyThread();
		Thread thread1=new Thread(myThread,"线程A");
		Thread thread2=new Thread(myThread,"线程B");
		thread1.start();
		thread2.start();
	}

}

线程各种状态
public class MyRunnable implements Runnable{

	@Override
	public void run() {
		System.out.println("线程t处于运行状态");
		try {
			Thread.sleep(500);
			System.out.println("线程t休眠500毫秒后继续运行");
		} catch (InterruptedException e) {
			System.err.println("线程t终止");
		}
	}

}

/**
 * 测试线程的各种状态
 * @author 30869
 *
 */
public class TestThread_state {

	public static void main(String[] args) {
		Thread thread=new Thread(new MyRunnable());
		System.out.println("线程t已创建");
		thread.start();
		System.out.println("线程t已就绪");
	}

}

线程同步(多线程):


同步方法

public class Synchronized_function implements Runnable {
	private static int count = 10;
	private static int num = 0;
	private static boolean flag = false;

	@Override
	public void run() {
		while (!flag) {
			sale();
		}
	}
	/**
	 * 同步方法
	 */
	public static synchronized void sale() {
		if (count <= 0) {
			flag = true;
			return;
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		num++;
		count--;
		System.out.println(Thread.currentThread().getName() + "抢到第" + num + "张票,剩余" + count + "张票");
	}
}

/**
 * 测试多线程同步方法
 * 
 * @author 30869
 *
 */
public class TestThread_synchronized_function {
	public static void main(String[] args) {
		Synchronized_function myThread = new Synchronized_function();
		Thread thread1 = new Thread(myThread, "黄牛党");
		Thread thread2 = new Thread(myThread, "抢票代理");
		Thread thread3 = new Thread(myThread, "桃跑跑");
		thread1.start();
		thread2.start();
		thread3.start();
	}
}

同步代码块

public class Synchronized_codeBlock implements Runnable {
	private int count = 10;
	private int num = 0;

	@Override
	public void run() {
		/**
		 * 同步代码块
		 */
		while (true) {
			synchronized (this) {
				if (count <= 0) {
					return;
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				num++;
				count--;
				System.out.println(Thread.currentThread().getName() + "抢到第" + num + "张票,剩余" + count + "张票");
			}
		}
	}
}

/**
 * 测试多线程同步代码块
 * @author 30869
 *
 */
public class TestThread_synchronized_codeBlock {

	public static void main(String[] args) {
		Synchronized_codeBlock myThread = new Synchronized_codeBlock();
		Thread thread1 = new Thread(myThread, "黄牛党");
		Thread thread2 = new Thread(myThread, "抢票代理");
		Thread thread3 = new Thread(myThread, "桃跑跑");
		thread1.start();
		thread2.start();
		thread3.start();
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值