---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
/*11-1
进程: 是一个正在执行中的程序。
每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个单元。
线程:就是进程中的一个独立的控制单元。
线程在控制着进程的执行。
一个进程中至少有一个线程。
java vm 启动时会有一个进程java.exe
该进程中至少有一个线程负责java程序的执行。
而且这个线程运行的代码存在于main方法中。
该线程称之为主线程
扩展:其实更细节的说明jvm,jvm启动不止一个线程,还有负责垃圾回收机制的线程。
*/
class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*11-2
如何在自定义的代码中,自定义一个线程?
通过api查找,java已经提供了对线程这类食物的描述。就是Thread。
创建线程的第一种方式:继承Thread
步骤:
1.定义类,继承Thread
2.复写Thread的run方法
目的:将自定义代码存储在run方法,让线程运行。
3.调用线程的start方法。
该方法两个作用:启动线程,调用run方法。
发现运行结果每次都不同。
因为多个线程都获取cpu的执行权。cpu执行到谁,谁就运行。
明确一点,在某一时刻,只能有一个程序在运行。(多核除外)
cpu 在做着快速切换,以达到看上去是同时运行的效果。
我们可以形象把多线程的运行行为在互相抢夺cpu的执行权。
这就是多线程的一个特性:随机性。谁抢到,谁执行。至于执行多长,cpu说了算。
*/
class Demo extends Thread
{
public void run(){
for(int i = 0; i < 60; i++)
System.out.println("hello");
}
}
class
{
public static void main(String[] args)
{
Demo d = new Demo;
d.start();
//d.run();
for (int i=0;i<60 ;i++ )
System.out.println("Hello World!");
}
}
/*11-3
为什么要覆盖run方法?
Thread类用于描述线程
该类定义了一个功能,用于存储线程要执行的代码,该功能就是run方法。
也就是说Thread类中的run方法,用于存储线程要运行的代码。
*/
/* 11-6 获取线程以及名称
线程都有自己默认的名称
Thread-编号 该编号从0开始。
static Thread CurrentThread();获取当前线程对象。
getName(); 获取线程名称。
设置线程名称:setName或者构造函数。
Thread.currentThread()=this;(this.getName());
*/
class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*11-7
需求:简单的卖票程序。
多个窗口卖票。
*/
/*11-8 实现Runnable接口
步骤:
1.定义类实现Runnable接口。
2.覆盖Runnable接口中的run方法。
将线程要运行的代码存放在该run方法中。
3.通过Thread类建立线程对象。
4.将Runnable接口的子类对象作为实际参数传递给Thread类的构造函数。
为什么要将Runnable接口的子类对象传递给Thread的构造函数?
因为,自定义的run方法所属的对象是Runnable接口的子类,所以要让线程
去指定对象的run方法。
5.调用Thread类的start方法开启线程并调用Runnable接口子类的run方法。
实现方式和家长方式有什么区别?
实现的好处:避免了单继承的局限性(可以继承其他类,不只是Thread)
在定义线程是,建立使用实现方式。
两种方式的区别:
继承Thread: 线程代码存放Thread子类run方法中。
实现Runnable:线程代码存放接口子类的run方法。
*/
/*11-9 多线程安全问题
通过分析,发现打印出0,-1,-2等票
多线程 的运行出现安全问题。
*/
class Ticket implements Runnable //extends Thread //因为Thread有实现Runnable接口,
//所以不实现Runnable接口也可以多个窗口卖同一组票,
//直接用extends Thread, new Thread(new Ticket(){覆盖run方法}).start().
//区别在与实现Runnable可以继承其他类
{
private int tick = 100;
public void run(){
while (tick>0)
{
System.out.println(Thread.currentThread().getName()+"sale: " + tick--);
}
}
}
class Demo
{
public static void main(String[] args)
{
Ticket t1 = new Ticket();
//Ticket t2 = new Ticket();
//Ticket t3 = new Ticket();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
//t1.start();
//t2.start();
//t3.start();
//t1.run();
//t2.run();
//t3.run();
System.out.println("Hello World!");
}
}
/*11-9 多线程安全问题
通过分析,发现打印出0,-1,-2等票
多线程 的运行出现安全问题。
问题原因:
当多条语句在操作同一个线程共享数据时,一个线程对多条语句只执行了一部分,还没有执行完。
另一个线程参与进来,导致共享数据的错误。
解决方法:
对多条操作共享数据的语句,只能让一个线程执行完。在执行过程中,其他线程不可以参与执行。
*/
/*11-10:同步代码块
java对于多线程安全问题提供了专业的解决方式。
就是同步代码块。
synchronized(对象){
需要被同步的代码;
}
对象如同锁,持有锁的线程可以在同步中执行。
没有持有锁的线程即使获取cpu的执行权也进不去,因为没有获取锁。
火车上的卫生间
同步的前提:
1.必须要有2个或者2个以上的线程。
2.必须是多个线程使用同一个锁。
必须保证同步中只有一个线程在运行。
好处:解决了多线程的安全问题。
弊端:多个线程需要判断锁,较为消耗资源。
*/
class Ticket implements Runnable
{
private int tick = 100;
public void run(){
while (true)
{
synchronized(new Object()){
if (tick>0)
{
Thread.sleep(10);
System.out.println(Thread.currentThread().getName()+"sale: " + tick--);
}else
break;
}
}
}
}
class Demo
{
public static void main(String[] args)
{
Ticket t1 = new Ticket();
//Ticket t2 = new Ticket();
//Ticket t3 = new Ticket();
new Thread(t1).start();
new Thread(t1).start();
new Thread(t1).start();
//t1.start();
//t2.start();
//t3.start();
//t1.run();
//t2.run();
//t3.run();
System.out.println("Hello World!");
}
}
/*11-11
需求:
银行有一个金库。
有两个储户分别存300元,每次存100,存3次。
目的:该程序是否有安全问题,如果有,如何解决?
如何找问题:
1.明确哪些代码是多线程运行代码。
2.明确共享数据。
3.明确多线程运行代码中哪些语句是操作共享数据的?
*/
class Bank
{
private int sum;
public synchronized void add(int n){
//synchronized(new Object()){
sum = sum + n;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println("Sum="+sum);
//}
}
}
class Cus implements Runnable
{
private Bank b = new Bank();
public void run(){
for (int x=0;x<3 ;x++ )
{
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c = new Cus();
Thread t1 = new Thread(c);
Thread t2 = new Thread(c);
t1.start();
t2.start();
System.out.println("Hello World!");
}
}
/*11-12
同步函数用的是哪一个锁?
函数需要被对象定义。那么函数都有一个所属对象引用。就是this。
所以同步函数所用的锁是this。
通过该程序进行验证。
为了验证方便,所用2个线程来卖票
一个线程在同步代码块中
一个线程在同步函数中
都在执行卖票动作。
*/
/*11-13静态同步函数的锁是Class对象。
如果同步函数被静态修饰后,使用的锁是什么呢?
通过验证,发现不是this,因为静态方法中也不可以定义this。
静态进内存时,内存中没有本类对象,但是一定有该类对应的字节码文件对象。
类名.class 该对象的类型是Class。
静态的同步方法,使用的锁是该方法所在的类的字节码文件对象。类名.class
*/
class Ticket implements Runnable
{
boolean flag = true;
private int tick = 100;
Object obj = new Object();
public void run(){
if (flag)
{
while (true)
{ //Ticket.class
synchronized(obj){//当该参数写new Object时,每个线程进来都持有不同的对象锁
if (tick>0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"sale: "+"t--" + tick--);
}else
break;
}
}
}else{
while(true){
show();
if(tick<=0) break;
}
}
}
synchronized void show(){//static
if (tick>0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"sale: " +"s.."+ tick--);
}
}
}
class Demo
{
public static void main(String[] args) throws Exception
{
Ticket t1 = new Ticket();
//Ticket t2 = new Ticket();
//Ticket t3 = new Ticket();
new Thread(t1).start();
Thread.sleep(10);
//t1.flag = false;
new Thread(t1).start();
new Thread(t1).start();
//t1.start();
//t2.start();
//t3.start();
//t1.run();
//t2.run();
//t3.run();
System.out.println("Hello World!");
}
}
/*11-14
单例设计模式。
*/
//饿汉式:
class Single{
private static final Single s = new Single();
private Single(){};
public static Single getInstance(){
return s;
}
}
//懒汉式
class Single{
private static Single s = null;
private Single(){}
public synchronized static Single getInstance(){
if (s == null)
s = new Single();
return s;
}
}
class Single{
private static Single s = null;
private Single(){}
public static Single getInstance(){
if (s == null)
{
synchronized(Single.class){//该类所属的字节码文件对象
if (s == null)
//-->A
//-->B
s = new Single();
}
}
return s;
}
}
/*
懒汉式和饿汉式有什么不同?
懒汉式的特点在于实例的延迟加载。
懒汉式延迟加载的问题:多线程访问时存在安全问题。
怎么解决?
可以加同步来解决。用同步代码块和同步函数都行。
但是有些低效,用双重判断能解决效率问题
加同步是使用的锁是哪一个?
该类所属的字节码文件对象。
*/
class
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
/*11-15
死锁:
*/
class Ticket implements Runnable
{
boolean flag = true;
private int tick = 100;
Object obj = new Object();
public void run(){
if (flag)
{
while (true)
{ //Ticket.class
synchronized(obj){//当该参数写new Object时,每个线程进来都持有不同的对象锁
if (tick>0)
show();
else
break;
}
}
}else{
while(true){
show();
if(tick<=0) break;
}
}
}
synchronized void show(){//static
synchronized(obj){//当该参数写new Object时,每个线程进来都持有不同的对象锁
if (tick>0)
{
try{Thread.sleep(10);}catch (Exception e){}
System.out.println(Thread.currentThread().getName()+"sale: "+"t--" + tick--);
}
}
}
}
class Demo
{
public static void main(String[] args) throws Exception
{
Ticket t1 = new Ticket();
//Ticket t2 = new Ticket();
//Ticket t3 = new Ticket();
new Thread(t1).start();
Thread.sleep(10);
//t1.flag = false;
new Thread(t1).start();
new Thread(t1).start();
System.out.println("Hello World!");
}
}
//-------------------------------------
class Test implements Runnable
{
private boolean flag;
Test(boolean flag){
this.flag = flag;
}
public void run(){
if (flag)
{
synchronized(MyLock.locka){
System.out.println("if locka");
synchronized(MyLock.lockb){
System.out.println("if lockb");
}
}
}else{
synchronized(MyLock.lockb){
System.out.println("else lockb");
synchronized(MyLock.locka){
System.out.println("else locka");
}
}
}
}
}
class MyLock
{
static Object locka = new Object();
static Object lockb = new Object();
}
class DeadLockTest
{
public static void main(String[]args){
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
//个人死锁的例子----------------------------
public class Demo {
public static void main(String[] args) {
Count c = new Count();
System.out.println("start");
new Thread(new CountA(c)).start();
new Thread(new CountB(c)).start();
}
}
class Count{
int a = 0;
int b = 0;
public int countA(){
synchronized(Count.class){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("countA counted");
countC();
return ++a;
}
}
public int countB(){
synchronized(Count.class){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
countA();
return ++b ;
}
}
public int countC(){
synchronized(Count.class){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("countA counted");
return ++a;
}
}
}
class CountA implements Runnable{
Count c = null;
CountA(Count c){
this.c = c;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i =0; i<10;i++){
int num = c.countA() ;//+ c.countB();
System.out.println("countA = "+num);
}
}
}
class CountB implements Runnable{
Count c = null;
CountB(Count c){
this.c = c;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i =0; i<10;i++){
int num = c.countB() ;//+ c.countA();
System.out.println("countB = "+num);
}
}
}
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------