public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i<100; i++){
System.out.println(i);
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
MyThread myThread1=new MyThread();
// myThread.run();
// myThread1.run();
//void start():导致线程开始执行 Java虚拟机开始调用此线程的run方法
myThread.start();
myThread1.start();
}
}
如果打印结果仍然一样,说明cou太好了,把循环次数改为1000试试
设置和获取线程名称public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i<100; i++){
// System.out.println(i);
//String getName():返回此线程的名称
System.out.println(getName()+":"+i);
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
MyThread myThread1=new MyThread();
// myThread.run();
// myThread1.run();
//setName(String name):将此线程名称更改为参数name
myThread.setName("高铁");
myThread.setName("飞机");
//void start():导致线程开始执行 Java虚拟机开始调用此线程的run方法
myThread.start();
myThread1.start();
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
// MyThread myThread=new MyThread();
// MyThread myThread1=new MyThread();
//
myThread.run();
myThread1.run();
//
// //setName(String name):将此线程名称更改为参数name
// myThread.setName("高铁");
// myThread1.setName("飞机");
//
//
//
// //void start():导致线程开始执行 Java虚拟机开始调用此线程的run方法
// myThread.start();
// myThread1.start();
//static Thread currentThread():返回当前正在执行对象的引用
System.out.println(Thread.currentThread().getName());
}
}
线程优先级
public class ThreadDemo1 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
MyThread myThread1=new MyThread();
// myThread.run();
// myThread1.run();
//setName(String name):将此线程名称更改为参数name
myThread.setName("高铁");
myThread1.setName("飞机");
//public final int getPriority():返回此线程的优先级
System.out.println(myThread.getPriority());//5
System.out.println(myThread1.getPriority());//5
//public fina; void setPriority(int newPriority):更改此线程的优先级
// myThread.setPriority(100);//IllegalArgumentException异常 不能超出MAX_PRIORITY 不能低于MIN_PRIORITY 默认值NORM_PRIORITY
System.out.println(Thread.MAX_PRIORITY);//10
System.out.println(Thread.MIN_PRIORITY);//1
System.out.println(Thread.NORM_PRIORITY);//5
myThread.setPriority(10);
myThread1.setPriority(1);
//void start():导致线程开始执行 Java虚拟机开始调用此线程的run方法
myThread.start();
myThread1.start();
//static Thread currentThread():返回当前正在执行对象的引用
// System.out.println(Thread.currentThread().getName());
}
}
线程控制
public class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i<100; i++){
// System.out.println(i);
//String getName():返回此线程的名称
System.out.println(getName()+":"+i);
try {
//static void sleep(long millis):使当前正在执行的线程(暂停执行)指定的毫秒数
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
运行ThreadDemo1
线程声明周期 实现Runnable接口的方式实现多线程public class MyRunnable implements Runnable{
@Override
public void run() {
for(int i=0; i<100; i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
public class MyRunnableDemo {
public static void main(String[] args) {
MyRunnable myRunnable=new MyRunnable();
//创建Tread类对象 把MyRunnable对象作为构造方法的参数
//Thread(Runnable target)
// Thread t1=new Thread(myRunnable);
//Thread(Runnable target, String name)
Thread t1=new Thread(myRunnable,"高铁");
Thread t2=new Thread(myRunnable,"飞机 ");
//启动线程
t1.start();
t2.start();
}
}
案例:火车卖票
public class SellTicket implements Runnable{
private int ticket=100;
@Override
public void run() {
//相同的票出现了多次
while(true) {
//ticket=100
//t1 t2 t3
//假设t1抢到了CPU的执行权
if (ticket > 0) {
//通过sleep()方法实现出票时间
try {
Thread.sleep(100);
//t1休息100毫秒
//假设t2抢到CPU的执行权 运行到这里 休息100毫秒
//t3抢到CPU的执行权 运行到这里 休息100毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
//假设线程顺序按照顺序醒过来
//t1抢到CPU执行权 在控制台输出 窗口1正在出售第100张票
//t2抢到CPU执行权 在控制台输出 窗口2正在出售第100张票
//t3抢到CPU执行权 在控制台输出 窗口3正在出售第100张票
System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
ticket--;
//假设这三个线程还是按照顺序来 就执行了三次ticket-- *** 作 最终票数变成了97
}
}
}
}
public class SellTicketTest {
public static void main(String[] args) {
SellTicket sellTicket=new SellTicket();
Thread t1=new Thread(sellTicket,"窗口1");
Thread t2=new Thread(sellTicket,"窗口2");
Thread t3=new Thread(sellTicket,"窗口3");
//启动线程
t1.start();
t2.start();
t3.start();
}
}
线程同步
修改文件
public class SellTicket implements Runnable{
private int ticket=100;
private Object obj=new Object();
@Override
public void run() {
while(true) {
synchronized (obj) {
if (ticket > 0) {
//通过sleep()方法实现出票时间
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
ticket--;
}
}
}
}
}
同步方法
public class SellTicket implements Runnable{
private int ticket=100;
private Object obj=new Object();
private int x=0;
@Override
public void run() {
while(true) {
if (x%2==0) {
synchronized (obj) {
if (ticket > 0) {
//通过sleep()方法实现出票时间
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
ticket--;
}
}
}
else {
sellTicket();
}
x++;
}
}
private synchronized void sellTicket() {
if (ticket > 0) {
//通过sleep()方法实现出票时间
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
ticket--;
}
}
}
线程安全的类
- StringBuffer
- Vector
- Hashtable
修改文件
public class SellTicket implements Runnable{
private int ticket=100;
private Lock lock=new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (ticket > 0) {
//通过sleep()方法实现出票时间
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + ticket + "张票");
ticket--;
}
}finally {
lock.unlock();
}
}
}
}
生产者消费者模式概述
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)