笔试凉经
选择题有一个重建二叉树,和一个堆给出进入的顺序,选不可能的出堆顺序(此题意思是先入堆的数字可能还没放新的数字就出去了,考虑到这一点就明白了)
另外就是一些类似牛客专项练习的java基础知识
另外有socket监听服务器的时候是socket还是socket server,这点确实不了解socket相关的东西
大题两个线程打印字母和文字
想到了用notify和wait,但是忘了多线程咋写了,写了两个四不像
还有一个脑筋急转弯是一个金条切两刀如何发七天工资,要求每天都有工资发,一直在想怎么两刀切七块,原来意思是领工资的时候可以拿前天小金条换一个大金条这种
结束后查到的参考代码如下
package cn.bridgeli.demo; import com.google.common.collect.Lists; import java.util.List; /** * @author BridgeLi * @date 2021/2/6 16:14 */ public class Thread_Communication_Notify_Wait { public static void main(String[] args) { final Object o = new Object(); final List<Integer> integers = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7); final List<String> strings = Lists.newArrayList("A", "B", "C", "D", "E", "F", "G"); new Thread(() -> { synchronized (o) { integers.forEach(item -> { System.out.print(item); o.notify(); try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }); o.notify(); } }, "t1").start(); new Thread(() -> { synchronized (o) { strings.forEach(item -> { System.out.print(item); o.notify(); try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }); o.notify(); } }, "t2").start(); } }
public class testWait { public static void main(String[] args) { testWait testWait = new testWait();testWait.soutNumber(); testWait.soutChar(); } public synchronized void soutNumber() { for (int i = 0; i < 28; i++) { System.out.println(i*2); System.out.println(i*2+1); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } this.notifyAll(); } } public synchronized void soutChar() { for (int i = 0; i < 28; i++) { char a= (char) ('a'+i); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(a); this.notifyAll(); } } }
public class testWait { public static void main(String[] args) { new Thread(new SoutNumber()).start(); new Thread(new SoutChar()).start(); } static class SoutNumber implements Runnable { @Override public synchronized void run() { for (int i = 0; i < 28; i++) { System.out.println(i*2); System.out.println(i*2+1); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } this.notifyAll(); } } } static class SoutChar implements Runnable { @Override public synchronized void run() { for (int i = 0; i < 28; i++) { char a= (char) ('a'+i); try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(a); this.notifyAll(); } } } }
#研发笔试##笔经#