我正在尝试使用基于“Rox Java NIO 教程”中的 java NIO(非阻塞)的服务器端代码。有很多传入的套接字连接,我只想接受 100 个。因此,如果有 100 个活动连接,则应拒绝/拒绝新的连接。但如何做到这一点呢?只有方法 ServerSocketChannel.accept() 返回 SocketChannel 对象。使用该对象我可以调用 socketChannel.socket().close(),但连接已经打开。这是代码的一部分:
@Override
public void run() {
while (true) {
try {
// Wait for an event one of the registered channels
this.selector.select();
// Iterate over the set of keys for which events are available
Iterator selectedKeys = this.selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = (SelectionKey) selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
}
// Check what event is available and deal with it
if (key.isAcceptable()) {
this.accept(key); …
Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关PhantomReference https://www.baeldung.com/java-phantom-reference的文章,并在此处找到了简化的示例代码:
public static void main(String[] args) throws InterruptedException {
ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
Object object = new Object();
PhantomReference<Object> phantomReference = new PhantomReference<>(object, referenceQueue);
object = null;
System.gc();
Thread.sleep(1_000);
System.out.println("isEnqueued() after GC: " + phantomReference.isEnqueued());
Reference reference = referenceQueue.poll();
if(reference != null) {
System.out.println("isEnqueued() after poll(): " + phantomReference.isEnqueued());
}
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
isEnqueued() after GC: true
isEnqueued() after poll(): false
Run Code Online (Sandbox Code Playgroud)
因此,一切工作都按预期进行,将对对象的强引用设置为null(GC会将其检测到)并将幻像引用添加到队列中。
现在在那篇文章中,他们说:“垃圾收集器在执行其引用的finalize方法之后,将一个幻像引用添加到引用队列。这意味着该实例仍在内存中。”
所以我想进行测试并覆盖finalize方法,例如:
Object object = new Object() {
@Override
protected void finalize() throws Throwable {
System.out.println("finalize()"); …
Run Code Online (Sandbox Code Playgroud) 我有real
列类型带有示例值的表:
123456,12
0,12345678
Run Code Online (Sandbox Code Playgroud)
和存储过程中的代码:
CREATE OR REPLACE FUNCTION test3()
RETURNS integer AS
$BODY$
DECLARE
rec RECORD;
BEGIN
FOR rec IN
SELECT
gme.abs_km as km,
CAST(gme.abs_km as numeric) as cast,
round(gme.abs_km:: numeric(16,2), 2) as round
FROM gps_entry gme
LOOP
RAISE NOTICE 'Km: % , cast: % , round: %', rec.km, rec.cast, rec.round;
INSERT INTO test (km, casting, rounding) VALUES (rec.km, rec.cast, rec.round);
END LOOP;
RETURN 1;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
Run Code Online (Sandbox Code Playgroud)
输出如下:
2014-02-05 12:49:53欧洲中部通知:公里:0.12345678,演员:0.123457,回合:0.12 2014-02-05 12:49:53欧洲中部通知:公里:123456.12,演员:123456,回合:123456.00
具有列的数据库表NUMERIC(19,2)
: …