package com.atguigu.java_thread;
/*
* 创建一个子线程,完成1-100之间自然数的输出。同样地,主线程执行同样的操作
* 创建多线程的第一种方式:继承java.lang.Thread类
*
*/
//1.创建一个继承与Thread的类
class SubThread extends Thread {
// 2.重写Thread类的run()方法.方法内实现此子线程要完成的功能
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() +":"+ i);
}
}
}
public class TestThread {
public static void main(String[] args) {
//3.创建一个子类的对象
SubThread st = new SubThread();
SubThread st2 = new SubThread();
//4.调用线程的start():启动此线程:调用相应的run()方法
//一个线程只能够执行一次start()
//不能通过Thread实现类对象的run()去启动一个线程
st.start();
st2.start();
//st.start();
//st.run();
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() +":"+ i);
}
}
}
package com.atguigu.java_thread;
/*
* Thread的常用方法
* 1.start():启动线程并执行相应的run()方法
* 2.run():子线程要执行的代码放入run()方法中
* 3.currenThread():静态的,调取当前的线程
* 4.getName():获取此线程的名字
* 5.setName():设置此线程的名字
* 6.yield():调用此方法的线程释放当前CPU的执行权
* 7.join():在A线程中调用B线程的join()方法,表示:当执行到此方法,A线程停止执行,直到B线程执行完毕
* 8.isAlive():判断此线程是否存活
* 9.sleep(long l):显示的让当前线程睡眠l毫秒
* 10.线程通信:wait() notify() notifyAll()
*
* 设置线程的优先级
* getPriority() :返回线程优先值
* setPriority(int newPriority) :改变线程的优先级
* 线程创建时继承父线程的优先级
*
*/
//1.创建一个继承与Thread的类
class SubThread1 extends Thread {
// 2.重写Thread类的run()方法.方法内实现此子线程要完成的功能
public void run() {
for (int i = 1; i <= 100; i++) {
// try {
// Thread.currentThread().sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
}
}
}
public class TestThread1 {
public static void main(String[] args) {
SubThread1 st1 = new SubThread1();
st1.setName("子线程1");
st1.setPriority(Thread.MAX_PRIORITY);
st1.start();
Thread.currentThread().setName("--------主线程");
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
// if(i % 10 == 0){
// Thread.currentThread().yield();
// }
// if(i == 20){
// try {
// st1.join();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// System.out.println(st1.isAlive());
}
}
}
package com.atguigu.java_thread;
/*
* 创建多线程的方式二:通过实现的方式
*
* 对比继承的方式 VS实现的方式
* 1.联系:public class Thread implements Runnable
* 2.哪个方式好?实现的方式优于继承的方式
* >实现的方式避免了java单继承的局限性
* >如果多个线程要操作同一份资源(数据),更适合使用实现的方式
*
*/
//1.创建一个实现了Runnable接口的类
class Print implements Runnable{
//2.实现接口的抽象方法
public void run() {
//子线程执行的代码
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() +":"+ i);
}
}
}
public class TestThread2 {
public static void main(String[] args) {
//3.创建一个Runnable接口实现类的对象
Print p = new Print();
//p.start();
//p.run();
//要想启动多线程必须调用start()方法
//4.将此对象作为形参传递给Thread类的构造器中,创建Thread类的对象,此对象即为一个线程
Thread t1 = new Thread(p);
//5.调用start()方法:启动线程并执行run()方法
t1.start();//启动线程:执行Thread对象生成时构造器形参的构造方法
//再创建一个线程
Thread t2 = new Thread(p);
t2.start();
}
}
package com.atguigu.java_thread;
//模拟火车站售票窗口,开启三个窗口售票,总票数为100张
//存在线程的安全问题
class WindowThread extends Thread{
static int ticket = 100;//申明成静态类变量,三个对象共用
public void run(){
while(true){
if(ticket > 0){
System.out.println(Thread.currentThread().getName() +"售票,票号为:" + ticket--);
}else{
break;
}
}
}
}
public class TestWindow {
public static void main(String[] args) {
WindowThread w1 = new WindowThread();
WindowThread w2 = new WindowThread();
WindowThread w3 = new WindowThread();
w1.setName("窗口一");
w2.setName("窗口二");
w3.setName("窗口三");
w1.start();
w2.start();
w3.start();
}
}
package com.atguigu.java_thread;
//使用实现Runnable接口的方式,售票
class WindowThread1 extends Thread {
int ticket = 100;// 共享数据
public void run() {
while (true) {
if (ticket > 0) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket--);
} else {
break;
}
}
}
}
public class TestWindow1 {
public static void main(String[] args) {
WindowThread2 w = new WindowThread2();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口一");
t2.setName("窗口二");
t3.setName("窗口三");
t1.start();
t2.start();
t3.start();
}
}
package com.atguigu.java_thread;
//使用实现Runnable接口的方式,售票
/*
* 此程序存在线程的安全问题:打印车票时会出现重票、错票
* 1.线程安全问题存在的原因?
* 由于一个线程在操作共享数据过程中,未执行完毕的情况下,另外的线程参与进来,
* 导致共享数据出现的安全问题
*
* 2.如何解决线程的安全问题?
* 必须让一个线程操作共享数据完毕以后,其它线程才有机会参与共享数据的操作。
*
* 3.java如何实现线程的安全:线程的同步机制
*
* 方式一:同步代码块
* synchronized(同步监视器){
* //需要被同步的代码块(即为操作共享数据的代码)
* }
* 1.共享数据:多个线程共同操作的同一个数据(变量)
* 2.同步监视器:由任何一个类的对象来充当。哪个线程获取此监视器,谁就能操作大括号里被同步的代码,俗称:锁
* 要求:所有的线程必须共用同一把锁!
* 注:在实现的方式中,考虑同步的话,可以使用this来充当锁,
* 但是在继承的方式中慎用this
*
* 方式二:同步方法
*
*
*/
class WindowThread2 extends Thread{
int ticket = 100;//共享数据
//Object obj = new Object();
public void run(){
//Animal a = new Animal();//局部变量
while(true){
// synchronized (obj) {
synchronized (this) {//this表示当前对象,本题中表示w
if(ticket > 0){
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +"售票,票号为:" + ticket--);
}else{
break;
}
}
}
}
}
public class TestWindow2 {
public static void main(String[] args) {
WindowThread2 w = new WindowThread2();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口一");
t2.setName("窗口二");
t3.setName("窗口三");
t1.start();
t2.start();
t3.start();
}
}
package com.atguigu.java_thread;
//模拟火车站售票窗口,开启三个窗口售票,总票数为100张
//存在线程的安全问题
class WindowThread3 extends Thread{
static int ticket = 100;//申明成静态类变量,三个对象共用
static Object obj = new Object();
public void run(){
while(true){
//synchronized (this) {//本题中this表示:w1 w2 w3
synchronized (obj) {
if(ticket > 0){
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() +"售票,票号为:" + ticket--);
}else{
break;
}
}
}
}
}
public class TestWindow3 {
public static void main(String[] args) {
WindowThread3 w1 = new WindowThread3();
WindowThread3 w2 = new WindowThread3();
WindowThread3 w3 = new WindowThread3();
w1.setName("窗口一");
w2.setName("窗口二");
w3.setName("窗口三");
w1.start();
w2.start();
w3.start();
}
}
package com.atguigu.java_thread;
//使用实现Runnable接口的方式,售票
/*
* 此程序存在线程的安全问题:打印车票时会出现重票、错票
* 1.线程安全问题存在的原因?
* 由于一个线程在操作共享数据过程中,未执行完毕的情况下,另外的线程参与进来,
* 导致共享数据出现的安全问题
*
* 2.如何解决线程的安全问题?
* 必须让一个线程操作共享数据完毕以后,其它线程才有机会参与共享数据的操作。
*
* 3.java如何实现线程的安全:线程的同步机制
*
* 方式一:同步代码块
* synchronized(同步监视器){
* //需要被同步的代码块(即为操作共享数据的代码)
* }
* 1.共享数据:多个线程共同操作的同一个数据(变量)
* 2.同步监视器:由任何一个类的对象来充当。哪个线程获取此监视器,谁就能操作大括号里被同步的代码,俗称:锁
* 要求:所有的线程必须共用同一把锁!
* 注:在实现的方式中,考虑同步的话,可以使用this来充当锁,
* 但是在继承的方式中慎用this
*
* 方式二:同步方法
* 将操作共享数据的方法声明为synchronized。即此方法为同步方法,能够保证当其中一个线程
* 执行此方法时,其它线程在外等待直至此线程执行完此方法。
*
* 4.线程的同步的弊端:由于同一个时间内只能有一个线程访问共享数据,效率变低了。
*/
class WindowThread4 extends Thread{
int ticket = 100;//共享数据
public void run(){
while(true){
show();
}
}
public synchronized void show() {
// synchronized (this) {// this表示当前对象,本题中表示w
if (ticket > 0) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket--);
}
// }
}
}
public class TestWindow4 {
public static void main(String[] args) {
WindowThread4 w = new WindowThread4();
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
Thread t3 = new Thread(w);
t1.setName("窗口一");
t2.setName("窗口二");
t3.setName("窗口三");
t1.start();
t2.start();
t3.start();
}
}
package com.atguigu.java_thread;
//线程通信:如下三个关键字使用的话,都得在同步代码块或同步方法中。
//wait():一旦一个i额线程执行到wait(),就释放当前的锁。
//notify() notifyAll():唤醒wait的一个或所有的线程
//使用两个线程打印1-100,线程1,线程2交替打印
class PrintNum implements Runnable {
int num = 1;
@Override
public void run() {
while (true) {
synchronized (this) {
notify();
if (num <= 100) {
try {
Thread.currentThread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" + num);
num++;
} else {
break;
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class TestCommunication {
public static void main(String[] args) {
PrintNum p = new PrintNum();
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
t1.setName("甲");
t2.setName("乙");
t1.start();
t2.start();
}
}