public class ThreadSafeDemo {
public int count = 0;
public void add(){
count++;
}
public static void main(String[] args) throws InterruptedException{
int size = 3;
ThreadSafeDemo threadSafeDemo = new ThreadSafeDemo();
CountDownLatch countDownLatch = new CountDownLatch(1);
for(int i=0;i{
try{
countDownLatch.await();
System.out.println(System.currentTimeMillis());
Thread.sleep(100);
}catch (Exception ex){
ex.printStackTrace();
}
}).start();
}
Thread.sleep(5000);
countDownLatch.countDown();
}
}
public class OldThread2 {
static volatile int tickts = 1;
public static void main(String[] args){
Thread t1 = new Thread(()->{
while (true){
if(tickts==1){
try {
Thread.sleep(100);
for(int i=0;i<10;i++){
System.out.println("a"+i);
}
}catch (InterruptedException ie){
ie.printStackTrace();
}
tickts=2;
return;
}
}
});
Thread t2 = new Thread(()->{
while (true){
if(tickts==2){
try {
Thread.sleep(100);
for(int i=0;i<10;i++){
System.out.println("b"+i);
}
}catch (InterruptedException ie){
ie.printStackTrace();
}
tickts=3;
return;
}
}
});
Thread t3 = new Thread(()->{
while (true){
if(tickts==3){
try {
Thread.sleep(100);
for(int i=0;i<10;i++){
System.out.println("c"+i);
}
}catch (InterruptedException ie){
ie.printStackTrace();
}
tickts=1;
return;
}
}
});
t1.start();
t2.start();
t3.start();
}
}
public class OldThread {
private static Semaphore s1 = new Semaphore(1);
private static Semaphore s2 = new Semaphore(1);
private static Semaphore s3 = new Semaphore(1);
public static void main(String[] args){
try {
s1.acquire();
s2.acquire();
}catch (InterruptedException ex){
ex.printStackTrace();
}
new Thread(()->{
while (true){
try {
s1.acquire();
}catch (InterruptedException iex){
iex.printStackTrace();
}
try {
Thread.sleep(500);
}catch (InterruptedException ex2){
ex2.printStackTrace();
}
System.out.println("A");
s2.release();
}
}).start();
new Thread(()->{
while (true){
try {
s2.acquire();
}catch (InterruptedException iex){
iex.printStackTrace();
}
try {
Thread.sleep(500);
}catch (InterruptedException ex2){
ex2.printStackTrace();
}
System.out.println("B");
s3.release();
}
}).start();
new Thread(()->{
while (true){
try {
s3.acquire();
}catch (InterruptedException iex){
iex.printStackTrace();
}
try {
Thread.sleep(500);
}catch (InterruptedException ex2){
ex2.printStackTrace();
}
System.out.println("C");
s1.release();
}
}).start();
}
}
6. 如何对一个字符串快速进行排序? 答:Fork/Join框架
public class MargeTest {
private static int MAX = 100;
private static int inits[] = new int[MAX];
//随机队列初始化
static {
Random r = new Random();
for(int index = 0; index taskResult = pool.submit(task);
try {
int[] ints = taskResult.get();
System.out.println(Arrays.toString(ints));
}catch (InterruptedException ex){
ex.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime-beginTime));
}
static class MyTask extends RecursiveTask{
private int source[];
public MyTask(int source[]){
this.source = source;
}
@Override
protected int[] compute() {
int sourcelen = source.length;
if(sourcelen>2){
int midIndex = sourcelen/2;
MyTask task1 = new MyTask(Arrays.copyOf(source,midIndex));
task1.fork();
MyTask task2 = new MyTask(Arrays.copyOfRange(source,midIndex,sourcelen));
task2.fork();
int result1[] = task1.join();
int result2[] = task2.join();
int mer[] = joinInts(result1,result2);
return mer;
}else{
if(sourcelen==1 || source[0]
7. TCP和UDP有什么区别?TCP为什么是三次握手,而不是两次?
答:TCP Transfer Control Protocol 是一种面向连接的,可靠的,传输层通信协议。 特点:好比打电话,面向连接的,点对点的通信,高可靠的,效率比较低,占用的系统资源比较多。 UDP User Datagram Protocol 是一种无连接的,不可靠的,传输层通信协议。 特点:好比广播,不需要连接,发送方不管接收方有没有准备好,直接发消息;可以进行广播发送。传输不可靠,有可能丢失消息;效率比较高,协议比较简单,占用的系统资源比较少。
TCP建立连接三次握手,断开连接四次挥手。如果是两次握手,可能造成连接资源浪费的情况。
8. Java有哪几种IO模型?有什么区别?
答:
BIO 同步阻塞IO。可靠性差,吞吐量低,适用于连接比较少且比较固定的场景。JDK1.4之前唯一的选择。编程模型最简单。
昨晚和朋友聊天,喝了点咖啡,由于我经常喝茶,很长时间没喝咖啡了,所以失眠了,于是起床读JVM规范,读完后在朋友圈发了一条信息:
JVM Run-Time Data Areas:The Java Virtual Machine defines various run-time data areas that are used during execution of a program. So
Spark SQL supports most commonly used features of HiveQL. However, different HiveQL statements are executed in different manners:
1. DDL statements (e.g. CREATE TABLE, DROP TABLE, etc.)
nginx在运行过程中是否稳定,是否有异常退出过?这里总结几项平时会用到的小技巧。
1. 在error.log中查看是否有signal项,如果有,看看signal是多少。
比如,这是一个异常退出的情况:
$grep signal error.log
2012/12/24 16:39:56 [alert] 13661#0: worker process 13666 exited on s
方法一:常用方法 关闭XML验证
工具栏:windows => preferences => xml => xml files => validation => Indicate when no grammar is specified:选择Ignore即可。
方法二:(个人推荐)
添加 内容如下
<?xml version=
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml&quo
最主要的是使用到了一个jquery的插件jquery.media.js,使用这个插件就很容易实现了。
核心代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.