标签:
1 package day23; 2 3 /** 4 * 5 * @author指针怒草内存栈 多线程第一种方式 6 * 7 */ 8 class Thread_01 extends Thread { 9 10 // 继承Thread类 11 // 重写run方法 12 public void run() { 13 for (int i = 1; i < 10; i++) { 14 System.out.println("线程"); 15 } 16 17 } 18 19 } 20 21 class Thread_01Test { 22 public static void main(String[] args) { 23 // 创建线程对象 24 Thread_01 thread_01 = new Thread_01(); 25 // 开启线程 26 thread_01.start(); 27 } 28 }
-------------------------------------------------------------------------------------------------------------------------------------------------------
1 package day23; 2 3 /** 4 * 5 * @author 指针怒草内存栈 演示getName() setName() 设置和获取线程的名字 6 * 7 */ 8 class MyThread extends Thread { 9 public void run() { 10 for (int i = 0; i < 10; i++) { 11 try { 12 Thread.sleep(1);//休眠一会 13 } catch (InterruptedException e) { 14 } 15 System.out.println(this.getName() + " .. " + i);//获取线程名字 16 } 17 } 18 } 19 20 public class Thread_02 { 21 public static void main(String[] args) { 22 MyThread mt = new MyThread(); 23 mt.setName("苍老师"); 24 mt.start(); 25 MyThread mt1 = new MyThread(); 26 mt1.setName("小泽老师"); 27 mt1.start(); 28 29 } 30 }
-----------------------------------------------------------------------------------------------------------------------------------------------------
线程的守护:
线程守护
--------------------------------------------------------------------------------------------------------------------------------------------------------
1 package day23; 2 3 /** 4 * 2.3、线程控制 2.3.1、线程休眠 2.3.2、优先级 2.3.3、守护线程 5 * 6 * @author 指针怒草内存栈 7 * 8 */ 9 public class SleepThread_01 { 10 /** 11 * sleep(long millis) 让线程休眠多少毫秒 12 * 13 */ 14 public static void main(String[] args) { 15 // new实现类对象(即任务类对象) 16 ThreadSleep tsSleep = new ThreadSleep(); 17 // new 线程类对象 18 Thread thread = new Thread(tsSleep, "柳岩岩"); 19 // 开启 20 thread.start(); 21 // new实现类对象(即任务类对象) 22 ThreadSleep tsSleep1 = new ThreadSleep(); 23 // new 线程类对象 24 Thread thread1 = new Thread(tsSleep, "范爷爷"); 25 // 开启 26 thread1.start(); 27 } 28 29 } 30 31 class ThreadSleep implements Runnable { 32 33 public void run() { 34 for (int i = 1; i < 10; i++) { 35 try { 36 Thread.sleep(100); 37 System.out.println(Thread.currentThread().getName() + "线程休眠"); 38 } catch (InterruptedException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 } 43 } 44 45 }
--------------------------------------------------------------------------------------------------------------------------------------------------------
1 package day23; 2 3 /** 4 * 2.3、线程控制 2.3.1、线程休眠 2.3.2、优先级 2.3.3、守护线程 Thread.yield();线程的礼让 5 * 6 * @author 指针怒草内存栈 7 * 8 */ 9 public class joinThread_2 { 10 /** 11 * sleep(long millis) 让线程休眠多少毫秒 12 * 13 * @throws Exception 14 * 15 */ 16 public static void main(String[] args) throws Exception { 17 // new实现类对象(即任务类对象) 18 ThreadSleep tsSleep = new ThreadSleep(); 19 Thread thread = new Thread(tsSleep, "柳岩岩-----thread"); 20 Thread thread2 = new Thread(tsSleep, "柳岩岩-####thread2"); 21 Thread thread1 = new Thread(tsSleep, "范爷爷%%%%%%thread1"); 22 // 开启 23 // 开启 24 thread.start(); 25 thread2.join();// 当这个线程加入时候,只有当这个线程全不运行结束才让其他线程运行 26 thread1.start(); 27 thread2.start(); 28 } 29 30 } 31 32 class Threadjoin implements Runnable { 33 34 public void run() { 35 for (int i = 1; i < 10; i++) { 36 try { 37 Thread.sleep(1); 38 System.out.println(Thread.currentThread().getName() + "线程休眠" + i); 39 } catch (InterruptedException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 }
--------------------------------------------------------------------------------------------------------------------------------------------------------
1 package day23; 2 3 /** 4 * 5 * @author指针怒草内存栈 线程安全问题 6 */ 7 public class SellTickets { 8 public static void main(String[] args) { 9 Tickes tickes = new Tickes(); 10 Thread t1 = new Thread(tickes, "窗口1"); 11 Thread t2 = new Thread(tickes, "窗口2"); 12 Thread t3 = new Thread(tickes, "窗口3"); 13 t1.start(); 14 t2.start(); 15 t3.start(); 16 } 17 } 18 19 class Tickes implements Runnable { 20 private int tickets = 100; 21 22 public void run() { 23 for (int i = 1; i < tickets; i++) { 24 System.out.println(Thread.currentThread().getName() + "------" + tickets); 25 tickets--; 26 } 27 } 28 29 }
1 package day23; 2 3 /**上面的那种方式或出现重复票!或者0票负数票! 4 * 5 * @author指针怒草内存栈 线程安全问题 使用同步代码块来解决重复票问题 6 */ 7 public class SellTickets2 { 8 public static void main(String[] args) { 9 Tickes2 tickes = new Tickes2(); 10 Thread t1 = new Thread(tickes, "窗口1"); 11 Thread t2 = new Thread(tickes, "窗口2"); 12 Thread t3 = new Thread(tickes, "窗口3"); 13 t1.start(); 14 t2.start(); 15 t3.start(); 16 } 17 } 18 19 class Tickes2 implements Runnable { 20 private int tickets = 100; 21 private Object obj = new Object(); 22 23 public void run() { 24 while (true) { 25 synchronized (obj) {//同步代码块
1 package day23; 2 3 /** 4 * 关于单例模式下的线程安全问题 5 * 6 * @author指针怒草内存栈 7 * 8 * 9 */ 10 public class Singleton { 11 public static void main(String[] args) { 12 SinSynchrozied.SinSynchr(); 13 } 14 15 } 16 17 class SinSynchrozied { 18 /** 19 * 懒汉式 20 */ 21 private SinSynchrozied() { 22 } 23 24 private static SinSynchrozied sin = null; 25 26 public static SinSynchrozied SinSynchr() { 27 if (sin == null) {//这里是为了提高效率。直接再外面就判断一次,如果不是,就直接跳过,不同进来了 28 // 同步 29 synchronized ("abc") { 30 if (sin == null) { 31 sin = new SinSynchrozied(); 32 } 33 } 34 35 } 36 return sin; 37 38 } 39 40 }
26 if (tickets > 0) { 27 System.out.println(Thread.currentThread().getName() + "------" + tickets); 28 tickets--; 29 } 30 } 31 32 } 33 34 } 35 }
1 package day23; 2 3 /** 4 * 死锁:其实就是不同的二把锁。交叉使用了。 5 * 6 * 在同步代码中。二个人不断的拿和放。本来以为很和谐,但是当cpu随机切换时候, 7 * 对方都持有对方的锁!导致谁也拿不到自己的锁! 8 * 9 * 10 * @author 指针怒草内存栈 11 * 12 */ 13 public class DeadLock implements Runnable { 14 // 定义二把锁 15 private Object lock1 = new Object(); 16 private Object lock2 = new Object(); 17 // 定义一个条件 18 private boolean flat = true; 19 20 @Override 21 public void run() { 22 // TODO Auto-generated method stub 23 if (flat == true) { 24 while (true) { 25 synchronized (lock1) { 26 27 synchronized (lock2) { 28 29 } 30 } 31 } 32 } else { 33 while (true) { 34 synchronized (lock2) { 35 36 synchronized (lock1) { 37 38 } 39 } 40 } 41 } 42 } 43 44 }
标签:
原文地址:http://www.cnblogs.com/java-synchronized/p/5574812.html