??进程是执行程序的一次执行过程,它是一个动态的概念,是系统资源分配的单位。通常一个进程中可以包含若干个线程,线程是CPU调度和执行的单位。
注意:
??很多多线程是模拟出来的,真正的多线程是指有多个CPU,即多核。如果是模拟出来的多线程,即在一个CPU的情况下,在同一个时间点,CPU只能执行一个代码,因为切换的很快,所以就有同时执行的错觉。
public class ThreadTest extends Thread{
public static void main(String[] args) {
ThreadTest test = new ThreadTest();
test.start();
for (int i = 0; i < 5; i++) {
System.out.println("这是主线程"+i);
}
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
// TODO Auto-generated method stub
System.out.println("这是分线程" + i);
}
}
}
这是主线程0
这是主线程1
这是分线程0
这是主线程2
这是分线程1
这是分线程2
这是主线程3
这是分线程3
这是主线程4
这是分线程4
public class RunnableTest implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("这是分线程" + i);
}
}
public static void main(String[] args) {
RunnableTest test = new RunnableTest();
Thread thread = new Thread(test);
thread.start();
for (int i = 0; i < 5; i++) {
System.out.println("这是主线程" + i);
}
}
}
这是主线程0
这是分线程0
这是分线程1
这是主线程1
这是分线程2
这是分线程3
这是主线程2
这是主线程3
这是分线程4
这是主线程4
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableTest implements Callable<Boolean>{
private static int ticketNum = 10;
private String name;
public CallableTest(){
super();
}
public CallableTest(String name){
this.name = name;
}
@Override
public Boolean call() {
while (true) {
if(ticketNum <= 0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "->拿到了第" + ticketNum-- + "票");
}
return true;
}
public static void main(String[] args) {
CallableTest test1 = new CallableTest("小明");
CallableTest test2 = new CallableTest("小红");
CallableTest test3 = new CallableTest("小花");
ExecutorService service = Executors.newFixedThreadPool(3); //创建执行服务
Future<Boolean> r1 = service.submit(test1); //提交执行
Future<Boolean> r2 = service.submit(test2);
Future<Boolean> r3 = service.submit(test3);
boolean result1;
boolean result2;
boolean result3;
try{
result1 = r1.get(); //获取结果
result2 = r2.get();
result3 = r3.get();
System.out.println("result1 = " + result1);
System.out.println("result2 = " + result2);
System.out.println("result3 = " + result3);
} catch (Exception e) {
e.printStackTrace();
} finally{
service.shutdownNow(); //关闭服务
}
}
}
小红->拿到了第10票
小花->拿到了第8票
小明->拿到了第9票
小明->拿到了第7票
小红->拿到了第5票
小花->拿到了第6票
小明->拿到了第3票
小红->拿到了第2票
小花->拿到了第4票
小花->拿到了第1票
小明->拿到了第-1票
小红->拿到了第0票
result1 = true
result2 = true
result3 = true
??Thread thread = new Thread();
??线程对象一旦创建就进入到新生状态
?Thread thread = new Thread();
?线程对象一旦创建就进入到新生状态
??当调用start()方法,线程立即进入就绪状态,但不意味着立即调度执行。
??进入运行状态,线程才真正执行线程体的代码块。
??当调用sleep(),wait()或同步锁时,线程进入阻塞状态,就是代码不往下执行,阻塞事件解除后,重新进入就绪状态,等待CPU调度执行。
??线程中断或者结束,一旦进入死亡状态,就不能再次启动。
1. setPriority(int newPriority) 更改线程的优先级
2. static void sleep(long millis) 在指定的毫秒数内让正在执行的线程对象休眠
3. void join() 等待该线程终止
4. static void yield() 暂停当前正在执行的线程对象,并执行其它线程
5. void interrupt() 中断线程
6. boolean isAlive() 测试线程是否处于活动状态
同步方法锁的是this
public class TicketTest implements Runnable{
private int ticketNum = 10;
boolean flag = true;
@Override
public void run() {
while (flag) {
try {
buy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void buy() throws InterruptedException{
if(ticketNum <= 0){
flag = false;
return;
}
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + "->拿到了第" + ticketNum-- + "票");
}
public static void main(String[] args) {
TicketTest ticket = new TicketTest();
new Thread(ticket,"小明").start();
new Thread(ticket,"老师").start();
new Thread(ticket,"黄牛党").start();
}
}
小明->拿到了第10票
老师->拿到了第9票
老师->拿到了第8票
老师->拿到了第7票
老师->拿到了第6票
老师->拿到了第5票
黄牛党->拿到了第4票
黄牛党->拿到了第3票
黄牛党->拿到了第2票
黄牛党->拿到了第1票
public class Bank {
public static void main(String[] args) {
Account account = new Account(100, "零钱");
Drawing boyFriend = new Drawing(account, 50, "boyFriend");
Drawing grilFriend = new Drawing(account, 100, "grilFriend");
boyFriend.start();
grilFriend.start();
}
}
class Account{
int money;
String name;
public Account(int money,String name){
this.money = money;
this.name = name;
}
}
class Drawing extends Thread{
Account account;
int drawingMoney;
int nowMoney;
public Drawing(Account account,int drawingMoney,String name){
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
@Override
public void run() {
synchronized(account){
if(account.money-drawingMoney < 0){
System.out.println(Thread.currentThread().getName() + "钱不够了,取不了");
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.money = account.money-drawingMoney;
nowMoney = nowMoney+drawingMoney;
System.out.println(account.name + "余额为:" + account.money);
System.out.println(this.getName() + "手里的钱:" + nowMoney);
}
}
}
零钱余额为:50
boyFriend手里的钱:50
grilFriend钱不够了,取不了
public class DeadLock {
public static void main(String[] args) {
Makeup girl1 = new Makeup(0, "小娜");
Makeup girl2 = new Makeup(1, "小樱");
girl1.start();
girl2.start();
}
}
class Lipstick{
}
class Mirror{
}
class Makeup extends Thread{
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String girlName;
public Makeup(int choice,String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException{
if(choice == 0){
synchronized(lipstick){
System.out.println(this.girlName + "获得口红的锁");
Thread.sleep(1000);
synchronized(mirror){
System.out.println(this.girlName + "获得镜子的锁");
}
}
} else {
synchronized(mirror){
System.out.println(this.girlName + "获得镜子的锁");
Thread.sleep(2000);
synchronized(lipstick){
System.out.println(this.girlName + "获得口红的锁");
}
}
}
}
}
小娜获得口红的锁
小樱获得镜子的锁
Makeup girl1 = new Makeup(0, "小娜");
Makeup girl2 = new Makeup(1, "小樱");
girl1.start();
girl2.start();
}
}
class Lipstick{
}
class Mirror{
}
class Makeup extends Thread{
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;
String girlName;
public Makeup(int choice,String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void makeup() throws InterruptedException{
if(choice == 0){
synchronized(lipstick){
System.out.println(this.girlName + "获得口红的锁");
Thread.sleep(1000);
}
synchronized(mirror){
System.out.println(this.girlName + "获得镜子的锁");
}
} else {
synchronized(mirror){
System.out.println(this.girlName + "获得镜子的锁");
Thread.sleep(2000);
}
synchronized(lipstick){
System.out.println(this.girlName + "获得口红的锁");
}
}
}
}
小樱获得镜子的锁
小娜获得口红的锁
小娜获得镜子的锁
小樱获得口红的锁
原文:https://www.cnblogs.com/starsgrace/p/14814883.html