开启一个新线程
public class Demo02_Thread {
/**
* @param args
*/
public static void main(String[] args) {
MyThread mt = new MyThread(); //4,创建该类对象
//mt.run(); //并没有开启线程
mt.start(); //5,开启新线程
for(int i = 0; i < 10; i++) {
System.out.println("bb");
}
}
}
class MyThread extends Thread { //1,自定义类继承Thread
public void run() { //2,重写run方法
for(int i = 0; i < 10; i++) { //3,将要执行的代码写在run方法中
System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
}
}
}
使用runable创建线程
package cn.itcast.thread;
public class Demo03_Runnable {
/**
* @param args
*/
public static void main(String[] args) {
MyRunnable mr = new MyRunnable(); //4,创建Runnable接口的子类对象 mr = 0x0011
Thread t = new Thread(mr); //5,创建线程对象,将Runnable接口的子类对象以参数传递
//Runnable target = new MyRunnable();
t.start(); //6,开启线程
for(int i = 0; i < 1000; i++) {
System.out.println("bb");
}
}
}
class MyRunnable implements Runnable { //1,自定义类实现Runnable
@Override
public void run() { //2,重写run方法
for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中
System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
}
}
}
匿名类创建线程
package cn.itcast.thread;
public class Demo04_Thread {
/**
* @param args
*/
public static void main(String[] args) {
new Thread() { //1,继承Thread类
public void run() { //2,重写run方法
for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中
System.out.println("aaaaaaaaaaaaaaaaaaaaaa");
}
}
}.start(); //4,开启线程
new Thread(new Runnable() { //1,实现Runnable接口
@Override
public void run() { //2,重写run方法
for(int i = 0; i < 1000; i++) { //3,将要执行的代码写在run方法中
System.out.println("bb");
}
}
}).start(); //4,开启线程
}
}
线程名
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread("马哥") { //通过构造方法给名字赋值
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(this.getName() + "....aaaaaaaaaaaaaaaaaaaaaa");
}
}
};
Thread t2 = new Thread("辉哥") {
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(this.getName() + "....bb");
}
}
};
/*t1.setName("凤姐"); //通过setName()方法给名字赋值
t2.setName("芙蓉姐姐");*/
t1.start();
t2.start();
}
获取当前线程信息
package cn.itcast.thread;
public class Demo06_CurrentThread {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "....aaaaaaaaaaaaaaaaaaaaaa");
}
}
},"张三");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + "....bb");
}
}
},"李四");
//t1.setName("马哥");
//t2.setName("辉哥");
Thread.currentThread().setName("我是主线程"); //Thread.currentThread写在哪个线程里就代表哪个线程引用
System.out.println(Thread.currentThread().getName());
t1.start();
t2.start();
}
}
sleep
package cn.itcast.thread;
public class Demo07_Sleep {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
for(int i = 20; i >= 0; i--) {
Thread.sleep(1000); //线程的休眠
System.out.println("这是第" + i + "秒");
}
}
}
守护线程
守护线程的意思是当其它非守护线程执行完毕之后,守护线程自动退出。
package cn.itcast.thread;
public class Demo09_SetDaemon {
/**
* @param args
*/
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
for(int i = 0; i < 50; i++) {
System.out.println(getName() + "...aaaaaaaaaaaaaaaaa");
}
}
};
Thread t2 = new Thread() {
public void run() {
for(int i = 0; i < 2; i++) {
System.out.println(getName() + "...bb");
}
}
};
t1.setDaemon(true); //设置t1为守护线程
t1.start();
t2.start();
}
}
加入线程
加入线程的意思是,暂停等待其它线程执行。
package cn.cast;
@SuppressWarnings({ "rawtypes", "unchecked" }) // 去除黄色提示
public class HelloWorld {
static int a = 5;
/**
* @param args
*/
public static void main(String[] args) {
final Thread t1 = new Thread() {
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + "...aaaaaaaaaaaaaaaaa");
}
}
};
Thread t2 = new Thread() {
public void run() {
for (int i = 0; i < 50; i++) {
try {
// t1.join(); //插入线程,当前线程暂停
t1.join(3000); // 插入线程,当前线程暂停30毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + "...bb");
}
}
};
t1.start();
t2.start();
}
}
class MyThread extends Thread { // 1,自定义类继承Thread
public void run() { // 2,重写run方法
for (int i = 0; i < 100; i++) { // 3,将要执行的代码写在run方法中
HelloWorld.a++;
}
}
}
class MyThread1 extends Thread { // 1,自定义类继承Thread
public void run() { // 2,重写run方法
for (int i = 0; i < 100; i++) { // 3,将要执行的代码写在run方法中
// System.out.println("b");
}
}
}
synchronized
package cn.itcast.syn;
public class Demo1_Synchronized {
/**
* @param args
*/
public static void main(String[] args) {
final Printer p = new Printer();
final Printer p2 = new Printer();
new Thread() {
public void run() {
while(true) {
p.print1();
}
}
}.start();
new Thread() {
public void run() {
while(true) {
p.print2();
}
}
}.start();
}
}
class Printer {
//Demo d = new Demo();
/*
* 任意对象都可以当作锁对象
* 注意:匿名对象不可以当作锁对象,因为不能保证两个锁对象是同一个对象
* 非静态的同步方法,锁对象是this
* 静态的同步方法,锁对象是当前类的字节码对象
*/
Object obj = new Object();
public static void print1() {
//同步代码块
synchronized(Printer.class) { //获取锁
System.out.print("a");
System.out.print("b");
System.out.print("c");
System.out.print("d");
System.out.print("e");
System.out.print("\r\n");
} //释放锁
}
public static synchronized void print2() {
//synchronized(obj) {
System.out.print("1");
System.out.print("2");
System.out.print("3");
System.out.print("4");
System.out.print("\r\n");
//}
}
}
class Demo{}
死锁
package cn.itcast.syn;
public class Demo4_DeadLock {
/**
* @param args
* 尽量避免同步代码块的嵌套
*/
private static String s1 = "筷子左";
private static String s2 = "筷子右";
public static void main(String[] args) {
new Thread() {
public void run() {
while(true) {
synchronized(s1) {
System.out.println(getName() + "获取" + s1 +"等待" + s2);
synchronized(s2) {
System.out.println(getName() + "获取" + s2 + "开吃");
}
}
}
}
}.start();
new Thread() {
public void run() {
while(true) {
synchronized(s2) {
System.out.println(getName() + "获取" + s2 +"等待" + s1);
synchronized(s1) {
System.out.println(getName() + "获取" + s1 + "开吃");
}
}
}
}
}.start();
}
}