Java 线程

来源:互联网 发布:新兴重工查知的老婆 编辑:程序博客网 时间:2024/05/24 02:38

创建线程(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();//启动线程1myThread2.start();//启动线程2(自动线程)//myThread1.run();//属于调用方法,不存在自动线程,单线程,顺序执行//myThread2.run();}}


implements Runnable
/** * 测试Runnable接口 * @author 30869 * */public class MyRunnable implements Runnable {//实现Runnable接口@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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{@Overridepublic 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;@Overridepublic 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;@Overridepublic 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();}}




原创粉丝点击