Java多线程基础
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程基础相关的知识,希望对你有一定的参考价值。
多线程基础
01:
package CpuNumber;
public class CpuNumber {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
int cupNums = runtime.availableProcessors();
System.out.println(cupNums);//8
}
}
线程基本使用
01:
package CpuNumber;
public class Thread01 {
public static void main(String[] args) {
Cat cat = new Cat();
cat.start();
}
}
//run Threar类 实现了Runnable接口的run方法
class Cat extends Thread{
int cnt = 0;
//重写run方法
@Override
public void run() {
while(true) {
System.out.println("喵喵,我是小猫咪"+(++cnt));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (cnt==8) break;
}
}
}
案例01
01:
tip:
- 当main线程启动一个子程序Thread-0,主线程不会阻塞,会继续执行
- cat.start();启动线程->最终会执行cat的run方法
- cat.run();run方法就是一个普通的方法,没有真正的启动一个线程,就会把run方法执行完毕,才会向下执行
package CpuNumber;
public class Thread01 {
public static void main(String[] args) throws InterruptedException {
Cat cat = new Cat();
cat.start();//启动线程
//tip:当main线程启动一个子程序Thread-0,主线程不会阻塞,会继续执行
/*
1.public synchronized void start() {
...
start0();
...
}
2.start0(); 是本地方法,是JVM调用,底层是C/C++实现
真正实现多线程的效果,是start0()
private native void start0();
*/
System.out.println("main Thread name = "+Thread.currentThread().getName());
for (int i = 0;i<60;i++)
{
System.out.println("main = "+i);
Thread.sleep(1000);
}
}
}
//run Threar类 实现了Runnable接口的run方法
class Cat extends Thread{
int cnt = 0;
//重写run方法
@Override
public void run() {
while(true) {
System.out.println("喵喵,我是小猫咪"+(++cnt)+"Thread name = "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (cnt==80) break;
}
}
}
案例02
01:
package CpuNumber;
public class Thread02Demo {
public static void main(String[] args) {
Dog dog = new Dog();
Thread thread = new Thread(dog);
thread.start();
Tiger tiger = new Tiger();
ThreadProxy threadProxy = new ThreadProxy(tiger);
threadProxy.start();
}
}
//线程代理类,模拟了一个极其简单的Thread
class ThreadProxy implements Runnable//把Proxy类当做Thread
{
private Runnable target = null;
@Override
public void run() {
if (target!=null)
{
target.run();
}
}
public ThreadProxy(Runnable target)
{
this.target = target;
}
public void start()
{
start0();
}
public void start0()
{
run();
}
}
class Animal{}
class Tiger extends Animal implements Runnable{
@Override
public void run() {
System.out.println("老虎嗷嗷叫");
}
}
class Dog implements Runnable
{
int cnt = 0;
@Override
public void run() {
while(true)
{
System.out.println("小狗旺旺叫..hi"+(++cnt)+" "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (cnt==10) break;
}
}
}
01:
package CpuNumber;
public class Thread02Demo {
public static void main(String[] args) {
Dog dog = new Dog();
Thread thread = new Thread(dog);
thread.start();
Tiger tiger = new Tiger();
ThreadProxy threadProxy = new ThreadProxy(tiger);
threadProxy.start();
}
}
//线程代理类,模拟了一个极其简单的Thread
class ThreadProxy implements Runnable//把Proxy类当做Thread
{
private Runnable target = null;
@Override
public void run() {
if (target!=null)
{
target.run();
}
}
public ThreadProxy(Runnable target)
{
this.target = target;
}
public void start()
{
start0();
}
public void start0()
{
run();
}
}
class Animal{}
class Tiger extends Animal implements Runnable{
@Override
public void run() {
System.out.println("老虎嗷嗷叫");
}
}
class Dog implements Runnable
{
int cnt = 0;
@Override
public void run() {
while(true)
{
System.out.println("小狗旺旺叫..hi"+(++cnt)+" "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (cnt==10) break;
}
}
}
案例03
01:
package CpuNumber;
public class SellTicket {
public static void main(String[] args) {
// SellTicket01 sellTicket01 = new SellTicket01();
// SellTicket01 sellTicket02 = new SellTicket01();
// SellTicket01 sellTicket03 = new SellTicket01();
//
// //这里会出现票数超卖现象
// sellTicket01.start();
// sellTicket02.start();
// sellTicket03.start();
SellTicket02 sellTicket02 = new SellTicket02();
new Thread(sellTicket02).start();
new Thread(sellTicket02).start();
new Thread(sellTicket02).start();
}
}
class SellTicket01 extends Thread{
private static int ticketNum = 100;
@Override
public void run() {
while(true)
{
if (ticketNum <= 0)
{
System.out.println("Sell ticket ending");
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口 "+Thread.currentThread().getName()+"售出一张票"+" 属于票数:"+(--ticketNum));
}
}
}
class SellTicket02 implements Runnable
{
private int ticketNum = 100;
@Override
public void run() {
while (true) {
if (ticketNum <= 0) {
System.out.println("Sell ticket ending");
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口 " + Thread.currentThread().getName() + "售出一张票" + " 属于票数:" + (--ticketNum));
}
}
}
线程终止
01:
package CpuNumber;
public class ThreadExit {
public static void main(String[] args) throws InterruptedException {
T t = new T();
t.start();
System.out.println("main sleep 10 seconds");
Thread.sleep(10*1000);
t.setLoop(false);
}
}
class T extends Thread{
private int cnt =0 ;
private boolean loop = true;
@Override
public void run() {
while(loop)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("T ing..."+(++cnt));
}
}
public void setLoop(boolean loop)
{
this.loop = loop;
}
}
线程常用方法
01:
/**
* The minimum priority that a thread can have.
*/
public static final int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public static final int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public static final int MAX_PRIORITY = 10;
02:
package CpuNumber;
public class CPUDEMO999 {
public static void main(String[] args) {
TT t = new TT();
t.setName("laohan");
t.setPriority(Thread.MIN_PRIORITY);
System.out.println(t.getName());
t.start();
for (int i = 0;i<5;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hi"+i);
}
System.out.println(t.getName()+" priority = "+t.getPriority());
t.interrupt();//提前中断休眠
}
}
class TT extends Thread{
@Override
public void run() {
while(true) {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + "baozi" + i);
}
try {
System.out以上是关于Java多线程基础的主要内容,如果未能解决你的问题,请参考以下文章