线程的创建
package Example02;
public class Example02 {
public static void main(String[] args) {
MyThread myThrea = new MyThread();
myThrea.start();
while(true) {
System.out.println("main方法在运行");
}
}
}
class MyThread extends Thread{
public void run() {
while(true) {
System.out.println("MyThread类的RUN()方法在运行");
}
}
}
实现Runnable接口创建多线程
package Example03;
public class Example03 {
public static void main(String[] args) {
MyThread myThread = new MyThread();//创建MyThread实例对象
Thread thread =new Thread(myThread);//创建线程对象
thread.start();
while(true) {
System.out.println("main()方法在运行");
}
}
}
class MyThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("MyThread类反方法在运行");
}
}
线程的调度
线程的优先级
package Example04;
public class Example04 {
public static void main(String[] args) {
new TickWindow().start();//创建第一个线程对象TickWindow并kaiq
new TickWindow().start();//创建第一个线程对象TickWindow并kaiq
new TickWindow().start();//创建第一个线程对象TickWindow并kaiq
new TickWindow().start();//创建第一个线程对象TickWindow并kaiq
}
}
class TickWindow extends Thread{
private int tickets =10;
public void run() {
while(true) {
if(tickets>0) {
Thread th =Thread.currentThread();//获取当前线程
String th_name = th.getName();//获取当前线程的名字
System.out.println(th_name+"正在发售第"+tickets--+"张票");
}
}
}
}
线程休眠
package Example07;
/*
* 休眠
*/
public class Example07 {
public static void main(String[] args) {
//创建一个线程
new Thread(new Task()).start();
for(int i=1;i<=10;i++) {
try {
if(i ==5) {
Thread.sleep(2000);//当前线程休眠3秒
}else {
Thread.sleep(500);
}
System.out.println("main主线程正在输出:"+i);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
//定义线程的任务
class Task implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<=10;i++) {
try {
if(i ==3) {
Thread.sleep(2000);//当前线程休眠3秒
}else {
Thread.sleep(500);
}
System.out.println("线程一正在输出:"+i);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
线程让步:
package Example08;
/*
* 线程让步
*/
public class Example08 {
public static void main(String[] args) {
Thread t1 =new yieldThread("线程A");
Thread t2 =new yieldThread("线程B");
//开启两个线程
t1.start();
t2.start();
}
}
class yieldThread extends Thread{
//定义一个有参的构造方法
public yieldThread(String name) {
super(name);//调用父类的构造方法
}
public void run() {
for(int i=0;i<6;i++) {
System.out.println(Thread.currentThread().getName()+"---"+i);
if(i==3) {
System.out.println("线程让步");
Thread.yield(); // 线程运行到此,作出让步
}
}
}
}
线程插队;
package Example09;
/*
* 线程插队
*/
public class Example09 {
public static void main(String[] args) throws Exception {
Thread t =new Thread(new EmergencyThread(),"线程1");
t.start();//开启线程
for(int i=1;i<6;i++) {
System.out.println(Thread.currentThread().getName()+"输出"+i);
if(i==2) {
t.join(); //调用join方法
}
Thread.sleep(1000);//线程休眠1000毫秒
}
}
}
class EmergencyThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=1;i<6;i++) {
System.out.println(Thread.currentThread().getName()+"输出"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
多线程同步
线程同步安全问题:
package Example12;
/*
* 同步方法
*/
import Example12.TicketWindow;
public class Example12 {
public static void main(String[] args) {
TicketWindow task =new TicketWindow();//创建线程的任务类对象
new Thread(task,"窗口1").start();
new Thread(task,"窗口2").start();
new Thread(task,"窗口3").start();
new Thread(task,"窗口4").start();
}
}
class TicketWindow implements Runnable{
private int tickets =10;
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
sendTicket();
}
}
private void sendTicket() {//定义售票方法
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(tickets>0) {
System.out.println(Thread.currentThread().getName()+"--卖出的票"+tickets--);
}else {
System.exit(0);
}
}
}
死锁
package Example13;
/*
* 死锁问题
*/
public class Example13 {
public static void main(String[] args) {
DeadLockThread d1= new DeadLockThread(true);
DeadLockThread d2=new DeadLockThread(false);
new Thread(d1,"Chinese").start();
new Thread(d1,"American").start();
}
}
class DeadLockThread implements Runnable{
//定义boject类型的chopstickes锁对象
static Object chopsticks =new Object();
//定义boject类型的knifeAndFork锁对象
static Object knifeAndFork =new Object();
private boolean flag;//定义一个Boolean类型的变量
//构造方法
DeadLockThread(boolean flag){
this.flag= flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(flag) {
while(true) {
//chopsticks锁对象上的同步代码块
System.out.println(Thread.currentThread().getName()+"===if----chopstic");
synchronized (chopsticks) {
System.out.println(Thread.currentThread().getName()+"===if----chopsticks");
}
}
}
else {
while(true) {
synchronized (knifeAndFork) {
System.out.println(Thread.currentThread().getName()+"===if----knifeAndFork");
}
}
}
}
}