Skip to content

Instantly share code, notes, and snippets.

View bw2k's full-sized avatar

Benjamin Wang bw2k

View GitHub Profile
@wllmsash
wllmsash / assigning-static-ip-addresses-in-wsl2.md
Last active May 16, 2024 06:21
Assigning Static IP Addresses in WSL2

Assigning Static IP Addresses in WSL2

WSL2 uses Hyper-V for networking. The WSL2 network settings are ephemeral and configured on demand when any WSL2 instance is first started in a Windows session. The configuration is reset on each Windows restart and the IP addresses change each time. The Windows host creates a hidden switch named "WSL" and a network adapter named "WSL" (appears as "vEthernet (WSL)" in the "Network Connections" panel). The Ubuntu instance creates a corresponding network interface named "eth0".

Assigning static IP addresses to the network interfaces on the Windows host or the WSL2 Ubuntu instance enables support for the following scenarios:

public class SimpleRateLimiter {
private Semaphore semaphore;
private int maxPermits;
private TimeUnit timePeriod;
private ScheduledExecutorService scheduler;
public static SimpleRateLimiter create(int permits, TimeUnit timePeriod) {
SimpleRateLimiter limiter = new SimpleRateLimiter(permits, timePeriod);
limiter.schedulePermitReplenishment();
return limiter;
@define-private-public
define-private-public / HttpServer.cs
Last active May 15, 2024 12:09
A Simple HTTP server in C#
// Filename: HttpServer.cs
// Author: Benjamin N. Summerton <define-private-public>
// License: Unlicense (http://unlicense.org/)
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Threading.Tasks;
@rafaeltuelho
rafaeltuelho / jpa2-criteria-api-sample.md
Last active March 7, 2024 14:29
Just to remember a simple example of JPA 2 Criteria API usage...
  • to perform a simple SELECT * FROM PLANE;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Plane> criteria = cb.createQuery(Plane.class);
		Root<Plane> plane = criteria.from(Plane.class);
		criteria.select(plane);
		List<Plane> planes = em.createQuery(criteria).getResultList();
@AdamStelmaszczyk
AdamStelmaszczyk / MyBenchmark.java
Created June 25, 2015 21:44
Should I use Java's String.format() if performance is important?
package org.sample;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@serkan-ozal
serkan-ozal / gist:b6a9701801279736e5ec
Created February 8, 2015 11:54
A Hacky Way to Clean All Thread Local Variables of Current Thread
void cleanThreadLocalsOfCurrentThread() {
try {
// Get a reference to the thread locals table of the current thread
Thread thread = Thread.currentThread();
Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
threadLocalsField.setAccessible(true);
Object threadLocalTable = threadLocalsField.get(thread);
// Get a reference to the array holding the thread local variables inside the
// ThreadLocalMap of the current thread
@thomasdarimont
thomasdarimont / DefaultMethodProxyExample.java
Last active November 19, 2022 01:24
Examples for dynamic creating instances of interfaces with only default methods
package labs;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;