Programming
Articles, notes, and tutorials filed under Programming.
Latest articles
Showing 9-16 of 17Difference between wait() and sleep(), yield() in Java
The difference between wait() and sleep(), or between sleep() and yield(), is one of the oldest questions asked in Java interviews. All three pause the currently running thread, but they differ in who owns them, whether they release the lock, and how the thread wakes up again. TL;DR sleep() yield() wait() Defined in Thread Thread Object Static method? Yes Yes No Releases the lock/monitor? No No Yes Must hold the monitor (be in synchronized)? No No Yes Wakes up when timeout expires or interrupted the scheduler decides (maybe immediately) notify()/notifyAll(), timeout, or interrupt Typical use pause for a fixed time hint to give up the CPU coordination between threads JavaDoc Definition Thread Class public static void sleep(long millis[, int nanos]) throws InterruptedException
Object Initialization Order in Java
An object is a chunk of memory bundled with the code that manipulates memory. In the memory, the object maintains its state (the values of its instance variables), which can change and evolve throughout its lifetime. To get a newly-created object off to a good start, its newly-allocated memory must be initialized to a proper initial state. Here we take an in-depth look at the mechanisms Java uses to manage object initialization.
HashMap Internal Implementation Analysis in Java
HashMap is one of the most common topics in Java interviews, and “How does HashMap work?” has a deceptively simple one-line answer: “On the principle of hashing.” To say anything more, you need to know how hashing works and how HashMap applies it internally. This post assumes you already know the basics of using a HashMap; if not, start with the official Java docs. The walkthrough below uses the pre-Java-8 source as its baseline; wherever Java 8+ changed the implementation, an inline Note flags it.
How To Convert Byte[] Array To String in Java
It is common to convert a String into a byte[] array and back again. The catch is that how you convert depends on what the bytes actually are: human-readable text in some encoding, or arbitrary binary data such as the output of a cipher. Using the wrong approach silently corrupts your data. Note: This post has been updated to always specify a charset explicitly and to cover the binary-data case. The original advice — new String(bytes) — relies on the platform’s default charset, which makes the result differ from machine to machine, and it cannot round-trip binary data at all. Both pitfalls are addressed below.
Use SQLite in Qt
SQLite is a lightweight, file-based database that needs no separate server process, which makes it a perfect fit for desktop applications that want to store data locally. Qt ships with a built-in SQLite driver as part of its SQL module, so you can talk to a database without pulling in any third-party dependency. This post walks through the few steps required to set it up and run your first queries.
Convert Excel Date into Timestamp in PHP
Excel stores a date internally as a number of days since January 1, 1900. For example: “June 9th, 2011 10:30 AM” would be stored as “40703.4375”. 40703 is the number of full days since 01/01/1900 and 0.4375 represents the time (10.5/24 = 0.4375). When you process dates read from an Excel spreadsheet (e.g., using PHPExcel) you often want to convert them into a UNIX timestamp, i.e. a number of seconds elapsed since midnight of January 1, 1970 UTC.
Writing Your First Win32 Application with C++
Welcome to the first windows tutorial. In this tutorial, I’ll give you the basic information on how to code in windows and we’ll go over a few important details first. Messages The basic windows program has two important functions inside. The first one is the event handler(I’ll talk about this one later on) and the second one is the main function(WinMain from now on). WinMain is similar to DOS’ main function, in fact, WinMain is called from the DOS main function but in windows, we don’t have access to the DOS main; everything is covered up by windows. Windows introduces us to messages. A message could be easily described as a pile of data that gets sent to the event handler which then tells the window what to do. A message’s name is prefixed with WM(stands for Windows Message as you might have guessed?). There are a whole lot of messages that a window can handle, however, we’ll only need a few of them. You can use these messages in many ways. For example, the message WM_CREATE, can be used for initialization purposes, it gets sent to the window when it’s created, and WM_PAINT is used when you want to draw something on the screen, and so on, you get the image. Most of the messages get sent to the window automatically; for example, when you move your mouse around you send a message to windows, and any other actions like that send messages to windows. You can also send a message to your window yourself, but you’ll probably never need to.
C# - Run Application as Administrator Automatically
In this tutorial, you will learn how you can run your application as an administrator automatically using Visual C#2012. Adding an Application Manifest Start by opening your C# Windows forms application. Right click on your project name in the solution explorer and add a new item… Select an application manifest file: Click add. Requiring Administrator Privileges A new tab should open up, most of the codes in it are not important to us. There is one line however that we need to change: