发表于: 2018-01-05 23:29:19
2 624
今天完成的事情:
继续对thread进行学习,了解了同步以及线程的安全.
守护线程
package com;
/**
* @author Arike
* Create_at 2018/1/5 7:50
*/
/**
* 测试守护线程
*/
public class test3 {
public static void main(String[] args) {
Thread t1 = new Thread(){
@Override
public void run() {
for(int i = 0; i < 2; i++) {
System.out.println("主线程");
}
}
};
Thread t2 = new Thread(() -> {
for(int i = 0; i < 50; i++) {
System.out.println("守护线程");
}
});
t2.setDaemon(true);
t1.start();
t2.start();
}
}
线程的优先级\
package com;
/**
* @author Arike
* Create_at 2018/1/5 9:03
*/
/**
* 测试priority优先级 10最大 1最小
*/
public class test5 {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
for(int i = 0; i < 500; i++) {
System.out.println("低优先级");
}
});
Thread t2 = new Thread(()->{
for(int i = 0; i < 500; i++) {
System.out.println("高优先级");
}
});
t1.setPriority(1);
t2.setPriority(10);
t1.start();
t2.start();
}
}
synchronized的基础使用
package com;
/**
* @author Arike
* Create_at 2018/1/5 9:35
*/
/**
* 测试synchronized如何使用
*/
public class test6 {
public static void main(String[] args) {
new Thread(() -> {
while (true) {
synchronized (test6.class){
System.out.print("我");
System.out.print("爱");
System.out.print("你");
System.out.println();
}
}
}).start();
new Thread(() -> {
while (true) {
synchronized (test6.class){
System.out.print("很");
System.out.print("开");
System.out.print("心");
System.out.println();
}
}
}).start();
}
}
synchronized修饰方法
package com;
/**
* @author Arike
* Create_at 2018/1/5 10:16
*/
/**
* 该类用于测试synchronized非静态方法的锁对象是this当前对象.
*/
public class test7 {
public static void main(String[] args) {
demo d =new demo();
new Thread(()->{
while(true){
d.print1();
}
}).start();
new Thread(()->{
while (true){
d.print2();
}
}).start();
}
}
class demo{
public synchronized void print1(){
System.out.print("我");
System.out.print("爱");
System.out.print("你");
System.out.println();
}
public void print2(){
synchronized (this){
System.out.print("很");
System.out.print("开");
System.out.print("心");
System.out.println();
}
}
}
修饰静态方法
package com;
/**
* @author Arike
* Create_at 2018/1/5 10:39
*/
/**
* 静态同步方法的锁对象是该类的.class字节码对象
*/
public class test8 {
public static void main(String[] args) {
demo1 d = new demo1();
new Thread(() -> {
while (true) {
d.print1();
}
}).start();
new Thread(() -> {
while (true) {
d.print2();
}
}).start();
}
}
class demo1 {
public synchronized static void print1() {
System.out.print("我");
System.out.print("爱");
System.out.print("你");
System.out.println();
}
public static void print2() {
synchronized (demo1.class) {
System.out.print("很");
System.out.print("开");
System.out.print("心");
System.out.println();
}
}
}
使用implements Runnable的方式实现一个小demo
package com;
/**
* @author Arike
* Create_at 2018/1/5 11:11
*/
/**
* 用implements Runnable的方式模拟火车票4个窗口售卖.
*/
public class test9 {
public static void main(String[] args) {
ticket ticket =new ticket();
new Thread(ticket).start();
new Thread(ticket).start();
new Thread(ticket).start();
new Thread(ticket).start();
}
}
class ticket implements Runnable{
public static int tickets = 100;
@Override
public void run() {
while (true){
//这里如果不添加锁对象就会出现问题,必须同步.异步的话就会出现问题
//这里的锁对象使用 .class和this都可以,因为只生成了一个ticket对象
synchronized (""){
if(tickets==0){
break;
}
try {
Thread.sleep(10);//这是为了模拟多行代码执行,异步的话cpu就会被别的线程抢去.
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" "+"还剩余"+tickets--+"张票");
}
}
}
}
使用extends Thread的方式实现小demo
package com;
/**
* @author Arike
* Create_at 2018/1/5 14:11
*/
public class test10 {
public static void main(String[] args) {
new Mythread().start();
new Mythread().start();
new Mythread().start();
new Mythread().start();
}
}
class Mythread extends Thread{
private static int ticktes = 100;
@Override
public void run() {
while(true){
//这里如果不添加锁对象就会出现问题,必须同步.异步的话就会出现问题
//这里的锁对象只能使用 .class,因为生成了4个Mythread对象,如果使用this其实还是不同的四把锁.
//发现更好用的,使用String字符串..相同的字符串会指向同一个常量池已存在字符串..
synchronized ("锁") {
if(ticktes==0){
break;
}
try {
Thread.sleep(10);//这是为了模拟多行代码执行,异步的话cpu就会被别的线程抢去.
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName()+" "+"还剩余"+ticktes--+"张票");
}
}
}
}
写了一个死锁
package com;
/**
* @author Arike
* Create_at 2018/1/5 14:57
*/
public class test11 {
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
while (true) {
synchronized ("1") {
System.out.println(getName()+" "+"手持"+1+"等待"+2);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ("2") {
System.out.println(getName()+" "+"拿到"+2+"行动");
}
}
}
}
}.start();
new Thread(){
@Override
public void run() {
while (true) {
synchronized ("2") {
System.out.println(getName()+" "+"手持"+2+"等待"+1);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized ("1") {
System.out.println(getName()+" "+"拿到"+1+"行动");
}
}
}
}
}.start();
}
}
死锁这里我为什么要用sleep,就是为让cpu被第二个线程拿过去,造成资源抢夺.最直观明显.
下午还看了一下cookie和session的基本介绍,理论性的东西就不贴了.
明天计划的事情:
进行登陆功能的完成
遇到的问题:
死锁一开始给我绕进去了.
收获:
在项目里可以把多线程用进项目里了.
评论