Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mio4 committed Apr 23, 2019
1 parent 4715ed7 commit 4bdc4d1
Show file tree
Hide file tree
Showing 38 changed files with 206 additions and 642 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 过滤所有.class文件
# 过滤所有jar包
*.class
*.jar
*.jar
*.idea
File renamed without changes.
7 changes: 7 additions & 0 deletions Concurrency/src/hire/testAtomic/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hire.testAtomic;

public class test{
public static void main(String[] args){
System.out.println("?");
}
}
30 changes: 30 additions & 0 deletions Concurrency/src/hire/testAtomic/withoutAtomic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hire.testAtomic;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

//TODO 为什么不是Runnable接口的类都可以作为线程池参数,运行
class Count{
// private Integer count = 0;
private AtomicInteger count = new AtomicInteger(0);
public Integer getCount(){
// return count;
return count.get();
}
public void increase(){
// count++;
count.incrementAndGet();
}
}

public class withoutAtomic {
public static void main(String[] args){
ExecutorService service = Executors.newCachedThreadPool();
Count count = new Count();
for(int i=0; i < 100; i++){
service.execute(() -> count.increase());
}
System.out.println(count.getCount());
}
}
23 changes: 23 additions & 0 deletions Concurrency/src/hire/testRunnable/useRunnable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hire.testRunnable;

class Seller2 implements Runnable{
private int ticket = 5;
public void run(){
while(ticket > 0){
sellTicket();
}
}

private synchronized void sellTicket(){
System.out.println("current thread:" + Thread.currentThread() + " thread ticket = " + ticket);
ticket--;
}
}

public class useRunnable {
public static void main(String[] args){
Seller2 s = new Seller2();
new Thread(s).start();
new Thread(s).start();
}
}
21 changes: 21 additions & 0 deletions Concurrency/src/hire/testRunnable/useThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hire.testRunnable;

class Seller extends Thread{
private int ticket = 5;
public void run(){
while(true){
System.out.println("thread ticket = " + ticket);
ticket--;
if(ticket < 0){
break;
}
}
}
}

public class useThread {
public static void main(String[] args){
new Seller().start();
new Seller().start();
}
}
49 changes: 49 additions & 0 deletions Concurrency/src/hire/testThreadLocal/testThreadLocal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hire.testThreadLocal;

class Bank {
ThreadLocal<Integer> t = new ThreadLocal<Integer>(){
@Override
protected Integer initialValue(){
return 100;
}
};

public int get(){
return t.get();
}

public void set(){
t.set(t.get() + 10);
}
}

class Transfer implements Runnable{
Bank bank;

public Transfer(Bank bank){
this.bank = bank;
}

@Override
public void run(){
for(int i=0; i < 10; i++){
bank.set();
System.out.println(Thread.currentThread() + " " + bank.get());
}
}
}

public class testThreadLocal{
public static void main(String[] args) throws InterruptedException {
Bank bank = new Bank();
Transfer t = new Transfer(bank);
Thread t1 = new Thread(t);
t1.start();
Thread t2 = new Thread(t);
t2.start();
t1.join();
t2.join();
System.out.println(bank.get());
}
}

20 changes: 20 additions & 0 deletions Concurrency/src/hire/testVolatile/NoVisibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hire.testVolatile;

public class NoVisibility {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
@Override
public void run() {
while(!ready) {
Thread.yield();
}
System.out.println(number);
}
}
public static void main(String[] args) {
new ReaderThread().start();
number = 42;
ready = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap0;
package notes.Chap0;

public class Bank {
private int money;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap0;
package notes.Chap0;

public class PrintThread extends Thread{
private String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap0;
package notes.Chap0;

public class Printer implements Runnable{
private String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap0;
package notes.Chap0;

class MyThread extends Thread{
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap1;
package notes.Chap1;

public class Gate {
private int counter = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap1;
package notes.Chap1;

import java.util.Random;
import java.util.concurrent.Semaphore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap2;
package notes.Chap2;

import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap2;
package notes.Chap2;



Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap2;
package notes.Chap2;

public class Dmain {
private final static Long CALL_COUNT = 1000000000L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap2;
package notes.Chap2;

/**
* StringBuffer不是immutable类
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap2;
package notes.Chap2;

class t{
private final String s;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap3;
package notes.Chap3;

import java.util.LinkedList;
import java.util.Queue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap4;
package notes.Chap4;

import java.io.FileWriter;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap5;
package notes.Chap5;

import java.util.Random;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap5;
package notes.Chap5;

public class Bmain {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap5;
package notes.Chap5;

/**
* 测试Sleep方法被Interrupted方法打断操作
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap6;
package notes.Chap6;

import java.util.Random;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Chap7;
package notes.Chap7;

class Host{
private final Helper helper = new Helper();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package PlusB;
package notes.PlusB;

class Runner extends Thread{
private volatile boolean quit = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package PlusB;
package notes.PlusB;

class Something{
private int x = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package PlusB;
package notes.PlusB;


public class NoVisibility {
Expand Down
5 changes: 5 additions & 0 deletions Concurrency/src/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class test {
public static void main(String[] args){
System.out.println("??");
}
}
Loading

0 comments on commit 4bdc4d1

Please sign in to comment.