Java基础(极客)——21、Java多线程编程(补充)
2015-05-26 17:13
337 查看
package com.zhh.thread;
/**
* 2、Java多线程-线程的实现
* 线程的实现的两种方式
* 1继承Thread
* 2实现Runnable接口
*
*/
public class ThreadDemo1 {
public static void main(String[] args) {
//测试线程的并发执行
MyThread thread1 = new MyThread("线程A");//创建第一个线程
thread1.start();//开启一个线程使用start方法,开启第一个线程
MyThread thread2 = new MyThread("线程B");//创建第二个线程
thread2.start();//开启一个线程使用start方法,开启第二个线程
}
}
class MyThread extends Thread {
String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
for (int i = 0; i < 1000; i++) {
System.out.println("线程已经启动" + name + i);
}
}
}
package com.zhh.thread;
/**
*
* 2、Java多线程-线程的实现
* 线程的实现的两种方式
* 1继承Thread
* 2实现Runnable接口
*
*/
public class ThreadDemo2 {
public static void main(String[] args) {
//测试Runnable接口实现线程
//测试线程的并发执行
MyRunable myRunable1 = new MyRunable("线程A");
Thread thread1 = new Thread(myRunable1);
thread1.start();//只有Thread类才能启动线程,Thread类中有很多操作线程的方法
MyRunable myRunable2 = new MyRunable("线程B");
Thread thread2 = new Thread(myRunable2);
thread2.start();//只有Thread类才能启动线程,Thread类中有很多操作线程的方法
}
}
class MyRunable implements Runnable {
String name;
public MyRunable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程已经启动了" + name + i);
}
}
}
package com.zhh.thread;
/**
*
* 3、Java多线程-线程的状态
*
*/
public class ThreadDemo3 {
}
package com.zhh.thread;
/**
*
* 4、Java多线程-线程的常用方法
* 线程的常用方法都在Thread类中
* 常用方法
* 1)取得线程的名称
* getName();
* 2)取得当前线程对象
* currentThread()
* 3)判断线程是否启动
* isAlive();
* 4)线程的强行运行
* jion();
* 5)线程的休眠
* sleep();
* 6)线程的礼让
* yield();
*
*
*
*/
public class ThreadDemo4 {
public static void main(String[] args) {
// method2();
method3();
}
static void method1() {
MyRunnable runnable1 = new MyRunnable("线程A");
Thread thread1 = new Thread(runnable1);
thread1.start();
//System.out.println(thread1.isAlive());//判断线程是否启动
// thread1.start();
//System.out.println(thread1.isAlive());//判断线程是否启动
for (int i = 0; i < 50; i++) {
if (i > 10) {
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}//强制执行子线程
}
System.out.println("主线程" + i);
}
}
/**
* 线程的休眠
*/
private static void method2() {
MyRunnable runnable1 = new MyRunnable("线程A");
Thread thread1 = new Thread(runnable1);
thread1.start();
}
/**
* 线程的礼让
*/
private static void method3() {
MyRunnable2 runnable1 = new MyRunnable2("线程A");
Thread thread1 = new Thread(runnable1);
MyRunnable2 runnable2 = new MyRunnable2("线程B");
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
}
/**
* 定义线程,实现Runnable接口
*
*/
class MyRunnable implements Runnable {
String name;
public MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
//System.out.println("子线程运行了。。。");
// System.out.println("得到当前线程对象:" + Thread.currentThread());
// System.out.println("得到当前线程名字:" + Thread.currentThread().getName());
try {
Thread.sleep(1000);//线程的休眠
System.out.println("自线程启动了" + i + name);
if (i == 10) {
System.out.println("线程的礼让。。。");
Thread.yield();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class MyRunnable2 implements Runnable {
String name;
public MyRunnable2(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("线程" + i + name);
if (i == 10) {
System.out.println("线程的礼让。。。");
Thread.yield();//线程的礼让
}
}
}
}
package com.zhh.thread;
/**
*5、Java多线程-线程的优先级
* 优先级高的有可能先执行(不是一定先执行)
* MIN_PRIORITY=1
* NORM_PRIORITY=5
* MAX_PRIORITY=10
*
*/
public class ThreadDemo5 {
public static void main(String[] args) {
//从打印出来的结果来看,优先级高的有可能先执行(不是一定先执行)
MyRunnable5 myRunnable1 = new MyRunnable5("A");
MyRunnable5 myRunnable2 = new MyRunnable5("B");
MyRunnable5 myRunnable3 = new MyRunnable5("C");
Thread thread1 = new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);
Thread thread3 = new Thread(myRunnable3);
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.NORM_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
/**
*
*
*/
class MyRunnable5 implements Runnable {
String name;
public MyRunnable5(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println("当前线程的名称:" + Thread.currentThread().getName() + ":" + i + ":"
+ name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.zhh.thread;
/**
*6、Java多线程-线程同步
*synchronized修饰的方法或代码块都可同步
*格式
*代码块:
*synchronized(同步对象){同步的代码块}
*方法:
*synchronized void 方法名() {}
*
*三个窗口卖5票,资源共享
*
*/
public class ThreadDemo6 {
public static void main(String[] args) {
//开启三个线程代表3个窗口
MyThread06 thread06 = new MyThread06();
Thread thread1 = new Thread(thread06);
Thread thread2 = new Thread(thread06);
Thread thread3 = new Thread(thread06);
thread1.start();
thread2.start();
thread3.start();
/*如果不同步,每个线程执行5次
*
*/
}
}
/**
*创建子线程类
*
*/
class MyThread06 implements Runnable {
int ticket = 5; //定义5张票
@Override
public void run() {
//循环超过5次都可以
for (int i = 0; i < 5; i++) {
// //同步代码块,同步代码块和同步方法是等效的
// synchronized (this) {
// try {
// if (ticket > 0) {
// Thread.sleep(1000);
// System.out.println(ticket--);
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }
tell();
}
}
/**
* synchronized关键字把方法定义为同步方法
*
*/
public synchronized void tell() {
try {
if (ticket > 0) {
Thread.sleep(1000);
System.out.println(ticket--);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
7、Java多线程-线程生命周期
源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F
/**
* 2、Java多线程-线程的实现
* 线程的实现的两种方式
* 1继承Thread
* 2实现Runnable接口
*
*/
public class ThreadDemo1 {
public static void main(String[] args) {
//测试线程的并发执行
MyThread thread1 = new MyThread("线程A");//创建第一个线程
thread1.start();//开启一个线程使用start方法,开启第一个线程
MyThread thread2 = new MyThread("线程B");//创建第二个线程
thread2.start();//开启一个线程使用start方法,开启第二个线程
}
}
class MyThread extends Thread {
String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
for (int i = 0; i < 1000; i++) {
System.out.println("线程已经启动" + name + i);
}
}
}
package com.zhh.thread;
/**
*
* 2、Java多线程-线程的实现
* 线程的实现的两种方式
* 1继承Thread
* 2实现Runnable接口
*
*/
public class ThreadDemo2 {
public static void main(String[] args) {
//测试Runnable接口实现线程
//测试线程的并发执行
MyRunable myRunable1 = new MyRunable("线程A");
Thread thread1 = new Thread(myRunable1);
thread1.start();//只有Thread类才能启动线程,Thread类中有很多操作线程的方法
MyRunable myRunable2 = new MyRunable("线程B");
Thread thread2 = new Thread(myRunable2);
thread2.start();//只有Thread类才能启动线程,Thread类中有很多操作线程的方法
}
}
class MyRunable implements Runnable {
String name;
public MyRunable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程已经启动了" + name + i);
}
}
}
package com.zhh.thread;
/**
*
* 3、Java多线程-线程的状态
*
*/
public class ThreadDemo3 {
}
package com.zhh.thread;
/**
*
* 4、Java多线程-线程的常用方法
* 线程的常用方法都在Thread类中
* 常用方法
* 1)取得线程的名称
* getName();
* 2)取得当前线程对象
* currentThread()
* 3)判断线程是否启动
* isAlive();
* 4)线程的强行运行
* jion();
* 5)线程的休眠
* sleep();
* 6)线程的礼让
* yield();
*
*
*
*/
public class ThreadDemo4 {
public static void main(String[] args) {
// method2();
method3();
}
static void method1() {
MyRunnable runnable1 = new MyRunnable("线程A");
Thread thread1 = new Thread(runnable1);
thread1.start();
//System.out.println(thread1.isAlive());//判断线程是否启动
// thread1.start();
//System.out.println(thread1.isAlive());//判断线程是否启动
for (int i = 0; i < 50; i++) {
if (i > 10) {
try {
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}//强制执行子线程
}
System.out.println("主线程" + i);
}
}
/**
* 线程的休眠
*/
private static void method2() {
MyRunnable runnable1 = new MyRunnable("线程A");
Thread thread1 = new Thread(runnable1);
thread1.start();
}
/**
* 线程的礼让
*/
private static void method3() {
MyRunnable2 runnable1 = new MyRunnable2("线程A");
Thread thread1 = new Thread(runnable1);
MyRunnable2 runnable2 = new MyRunnable2("线程B");
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
}
/**
* 定义线程,实现Runnable接口
*
*/
class MyRunnable implements Runnable {
String name;
public MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
//System.out.println("子线程运行了。。。");
// System.out.println("得到当前线程对象:" + Thread.currentThread());
// System.out.println("得到当前线程名字:" + Thread.currentThread().getName());
try {
Thread.sleep(1000);//线程的休眠
System.out.println("自线程启动了" + i + name);
if (i == 10) {
System.out.println("线程的礼让。。。");
Thread.yield();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class MyRunnable2 implements Runnable {
String name;
public MyRunnable2(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("线程" + i + name);
if (i == 10) {
System.out.println("线程的礼让。。。");
Thread.yield();//线程的礼让
}
}
}
}
package com.zhh.thread;
/**
*5、Java多线程-线程的优先级
* 优先级高的有可能先执行(不是一定先执行)
* MIN_PRIORITY=1
* NORM_PRIORITY=5
* MAX_PRIORITY=10
*
*/
public class ThreadDemo5 {
public static void main(String[] args) {
//从打印出来的结果来看,优先级高的有可能先执行(不是一定先执行)
MyRunnable5 myRunnable1 = new MyRunnable5("A");
MyRunnable5 myRunnable2 = new MyRunnable5("B");
MyRunnable5 myRunnable3 = new MyRunnable5("C");
Thread thread1 = new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);
Thread thread3 = new Thread(myRunnable3);
thread1.setPriority(Thread.MIN_PRIORITY);
thread2.setPriority(Thread.NORM_PRIORITY);
thread3.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
/**
*
*
*/
class MyRunnable5 implements Runnable {
String name;
public MyRunnable5(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
System.out.println("当前线程的名称:" + Thread.currentThread().getName() + ":" + i + ":"
+ name);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.zhh.thread;
/**
*6、Java多线程-线程同步
*synchronized修饰的方法或代码块都可同步
*格式
*代码块:
*synchronized(同步对象){同步的代码块}
*方法:
*synchronized void 方法名() {}
*
*三个窗口卖5票,资源共享
*
*/
public class ThreadDemo6 {
public static void main(String[] args) {
//开启三个线程代表3个窗口
MyThread06 thread06 = new MyThread06();
Thread thread1 = new Thread(thread06);
Thread thread2 = new Thread(thread06);
Thread thread3 = new Thread(thread06);
thread1.start();
thread2.start();
thread3.start();
/*如果不同步,每个线程执行5次
*
*/
}
}
/**
*创建子线程类
*
*/
class MyThread06 implements Runnable {
int ticket = 5; //定义5张票
@Override
public void run() {
//循环超过5次都可以
for (int i = 0; i < 5; i++) {
// //同步代码块,同步代码块和同步方法是等效的
// synchronized (this) {
// try {
// if (ticket > 0) {
// Thread.sleep(1000);
// System.out.println(ticket--);
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }
tell();
}
}
/**
* synchronized关键字把方法定义为同步方法
*
*/
public synchronized void tell() {
try {
if (ticket > 0) {
Thread.sleep(1000);
System.out.println(ticket--);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
7、Java多线程-线程生命周期
源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F
相关文章推荐
- Java基础(极客)——18、Java数组(补充)
- Java基础(极客)——20、Java集合类详解(补充)
- Java基础(极客)——23、Java中的IO操作(补充)
- Java基础(极客)——24、Java中的HTTP通信(补充)
- Java基础(极客)——25、Java集合类详解(补充)
- Java基础(极客)——19、Java面向对象之泛型(补充)
- Java基础(极客)——22、Java中的JSON操作(补充)
- 【Java基础】(04)多线程编程
- java基础之细节补充
- Java面试题之Java基础部分 21-30题
- java语言基础(21)——面向对象(成员变量和局部变量的区别)
- Java基础知识强化21:java中length、length()、size()区别
- 传智播客 Java web基础补充
- Java基础学习21(代码块)
- Java基础(极客)——10、Java面向对象的基本特征之一:封装性
- Java基础-21总结字符流,IO流编码问题,实用案例
- 黑马程序员 21 Java基础加强-03-内省篇
- Java基础学习总结(21)——常用正则表达式列表
- Java基础知识强化之网络编程笔记21:Android网络通信之 Android常用OAuth登录(获取令牌信息)
- Java基础(极客)——源码、视频下载