多线程

两种方式开启线程
第一种:

package com.java.thread;
public class Demo2 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MyThread mt=new MyThread();
		mt.start();
		for(int i=0;i<1000;i++) {
			System.out.println("456");
		}
	}
}
class MyThread extends Thread{
	@Override
	public void run() {
		for(int i=0;i<1000;i++) {
			System.out.println("123");
		}
	}
}

第二种:

package com.java.thread;
public class Demo3 {
	public static void main(String[] args) {
		// 创建Runnable的子类对象
		MyRun myRun = new MyRun();
		//将其当做参数传递给Thread的构造函数
		Thread t = new Thread(myRun);
		t.start();
		for(int i=0;i<1000;i++) {
			System.out.println("456");
		}
	}
}
class MyRun implements Runnable{
	@Override
	public void run() {
		for(int i=0;i<1000;i++) {
			System.out.println("123");
		}
	}
}

匿名内部类实现开启线程

new Thread() {
			public void run() {
				for (int i = 0; i < 1000; i++) {
					System.out.println("1111");
				}
			}
		}.start();
		new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 1000; i++) {
					System.out.println("2222");
				}
			}
		}).start();

线程获取和设置名字

private static void demo1() {
	new Thread("yige") {
		public void run() {
			System.out.println(this.getName());
		}
	}.start();
}

获取当前线程对象

new Thread() {
public void run() {
	System.out.println(getName());
}
}.start();
new Thread(new Runnable() {
public void run() {
	//Thread.currentThread()获取当前执行线程
	System.out.println(Thread.currentThread().getName());
}
}).start();
Thread.currentThread().setName("主线程");
System.out.println(Thread.currentThread().getName());

休眠线程

for (int i = 20; i>=0; i--) {
	Thread.sleep(1000);
	System.out.println(i);
}

守护线程,线程结束,守护线程也结束(结束也需要一点时间)。

//设置守护线程
t2.setDaemon(true);
t1.start();
t2.start();

加入线程

//插队执行线程
t1.join();
//插队指定时间,过了指定时间之后两条线程交替执行(时间毫秒)
t1.join(1);

礼让线程

//让出CPU
Thread.yield();

设置线程优先级

//设置最大优先级
//t1.setPriority(Thread.MAX_PRIORITY);
t1.setPriority(10);
//设置最小优先级
//t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(1);

同步代码快

Demo d=new Demo();
public void print1() {
	//同步代码块,锁机制。所对象可以是任意的。锁对象不能用匿名对象,因为匿名对象不是同一个对象。
	synchronized (d) {
		System.out.println("1");
		System.out.println("11");
		System.out.println("111");
		System.out.println("1111");
		System.out.println("11111");
	}
}
public void print2() {
	synchronized (d) {
		System.out.println("2");
		System.out.println("22");
		System.out.println("222");
		System.out.println("22222");
		System.out.println("22222");
	}
}

同步方法

//非静态的同步方法的锁对象是this
public synchronized void print1() {
	System.out.println("1");
	System.out.println("11");
	System.out.println("111");
	System.out.println("1111");
	System.out.println("11111");
}
public void print2() {
	synchronized (this) {
		System.out.println("2");
		System.out.println("22");
		System.out.println("222");
		System.out.println("22222");
		System.out.println("22222");
	}
}
class Printer{
	Demo d=new Demo();
	//非静态的同步方法的锁对象是this
	//静态的同步方法的锁对象是该类的字节码对象
	public static synchronized void print1() {
		System.out.println("1");
		System.out.println("11");
		System.out.println("111");
		System.out.println("1111");
		System.out.println("11111");
	}
	public static void print2() {
		//同步代码块,锁机制。所对象可以是任意的。锁对象不能用匿名对象,因为匿名对象不是同一个对象。
		synchronized (Printer.class) {
			System.out.println("2");
			System.out.println("22");
			System.out.println("222");
			System.out.println("22222");
			System.out.println("22222");
		}
	}
}

死锁


    private static String s1="1";
	private static String s2="2";
	public static void main(String[] args) {
		new Thread() {
			public void run() {
				while(true) {
					synchronized (s1) {
						System.out.println(getName()+s1+"deng"+s2);
						synchronized (s2) {
							System.out.println(getName()+s2+"chi");
						}
					}
				}
			}
		}.start();
		//为了防止死锁,尽量不要进行同步代码块嵌套
		new Thread() {
			public void run() {
				while(true) {
					synchronized (s2) {
						System.out.println(getName()+s2+"deng"+s1);
						synchronized (s1) {
							System.out.println(getName()+s1+"chi");
						}
					}
				}
			}
		}.start();
	}

Vector是线程安全的,ArrayList是线程不安全的。
Stringbuffer是线程安全的,StingBuilder是线程不安全的。
Hashtable是线程安全的,HashMap是线程不安全的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值